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 the install fails before the lockfile is updated (for example because a package can’t be resolved), the manifest is restored to what it was before the command.

  • 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.

  • If a package is already listed through the patch: protocol, it keeps its patch: only the range of the patched package is replaced, unless the added range is itself a patch: range.

  • --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 (unless the range references a catalog, such as catalog: or catalog:<name>, in which case it’s kept as-is).

  • Packages that Yarn provides itself, such as @yarnpkg/node, aren’t published on the registry: asked for by name or with a bare range (@yarnpkg/node@^24), they’re added through the builtin: protocol.

  • 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 --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

Examples#

Add a regular package to the current workspace

yarn add lodash

Add a specific version for a package to the current workspace

yarn add lodash@4.17.21

Add a package to the devDependencies of the current workspace

yarn add -D typescript

Add a local package (gzipped tarball format) to the current workspace

yarn add local-package-name@file:../path/to/local-package-name-v0.1.2.tgz