yarn add — Yarn CLI reference

Add new dependencies to the project

Docs/Reference/CLI/add

yarn add

Add new dependencies to the project

yarn add <DESCRIPTORS…>

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 dependencies by default. Use -D,--dev to add it to devDependencies, or -P,--peer to add it to peerDependencies.

  • If a package is already listed in dependencies or devDependencies, it is upgraded in place. Peer dependencies are only changed when -P,--peer is used.

  • --prefer-dev behaves like a flexible -D,--dev: it adds new packages to devDependencies, but upgrades an existing dependencies entry in place instead of failing.

  • -O,--optional adds the package to optionalDependencies. Combined with -P,--peer, it marks the peer as optional through peerDependenciesMeta.

  • If the added package doesn’t specify a range, Yarn resolves the latest tag and uses the returned version to generate a semver range. The range uses the ^ modifier by default, unless defaultSemverRangePrefix, -T,--tilde, or -E,--exact selects a different modifier. There are two exceptions: workspace packages use their local version, and -P,--peer defaults to * without resolving the range.

  • If the added package specifies a range (such as ^1.0.0, latest, or rc), Yarn will add this range as-is in the resulting package.json entry (in particular, tags such as rc will 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-build will not run the build scripts at all. Note that this is different from setting enableScripts to 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-lockfile will 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.

#--fixed, -Fboolean

Store dependency tags as-is instead of resolving them to versions

#--exact, -Eboolean

Use an exact semver range for resolved versions

#--tilde, -Tboolean

Use the ~ semver modifier on the resolved range

#--caret, -Cboolean

Use the ^ semver modifier on the resolved range

#--peer, -Pboolean

Add a package as a peer dependency

#--dev, -Dboolean

Add a package as a dev dependency

#--optional, -Oboolean

Add or upgrade a package as an optional dependency or optional peer dependency

#--prefer-devboolean

Prefer devDependencies for new packages while preserving existing dependency fields

#--no-time-gateboolean

Disable the minimum release age check for this command

#--modeboolean

Select which install artifacts Yarn should generate

#--silentboolean

Hide any output but errors