yarn add — Yarn CLI reference
Add new dependencies to the project
yarn add
Add new dependencies to the project
This command adds packages to the nearest workspace manifest and then runs an install.
-
If a package wasn’t listed before, it is added to
dependenciesby default. Use-D,--devto add it todevDependencies, or-P,--peerto add it topeerDependencies. -
If a package is already listed in
dependenciesordevDependencies, it is upgraded in place. Peer dependencies are only changed when-P,--peeris used. -
--prefer-devbehaves like a flexible-D,--dev: it adds new packages todevDependencies, but upgrades an existingdependenciesentry in place instead of failing. -
-O,--optionaladds the package tooptionalDependencies. Combined with-P,--peer, it marks the peer as optional throughpeerDependenciesMeta. -
If the added package doesn’t specify a range, Yarn resolves the
latesttag and uses the returned version to generate a semver range. The range uses the^modifier by default, unlessdefaultSemverRangePrefix,-T,--tilde, or-E,--exactselects a different modifier. There are two exceptions: workspace packages use their local version, and-P,--peerdefaults to*without resolving the range. -
If the added package specifies a range (such as
^1.0.0,latest, orrc), Yarn will add this range as-is in the resulting package.json entry (in particular, tags such asrcwill be encoded as-is rather than being converted into a semver range).
If the --cached option is used, Yarn will preferably reuse the highest version already used somewhere within the project, even if through a transitive dependency.
If the -i,--interactive option is used, or if the preferInteractive setting is enabled, Yarn first checks whether other workspaces in the project already use the specified package and offers to reuse those ranges.
If the --mode=<mode> option is set, Yarn will change which artifacts are generated. The modes currently supported are:
-
skip-buildwill not run the build scripts at all. Note that this is different from settingenableScriptsto false because the latter will disable build scripts, and thus affect the content of the artifacts generated on disk, whereas the former will just disable the build step - but not the scripts themselves, which just won’t run. -
update-lockfilewill skip the link step altogether, and only fetch packages that are missing from the lockfile (or that have no associated checksums). This mode is typically used by tools like Renovate or Dependabot to keep a lockfile up-to-date without incurring the full install cost.
For a compilation of all the supported protocols, please consult the dedicated page from our website: https://yarnpkg.com/protocols.
Store dependency tags as-is instead of resolving them to versions
Use an exact semver range for resolved versions
Use the ~ semver modifier on the resolved range
Use the ^ semver modifier on the resolved range
Add a package as a peer dependency
Add a package as a dev dependency
Add or upgrade a package as an optional dependency or optional peer dependency
Prefer devDependencies for new packages while preserving existing dependency fields
Disable the minimum release age check for this command
Select which install artifacts Yarn should generate
Hide any output but errors