Installs a package and any packages that it depends on.
In general, a package is simply a folder with code and a
package.json file that describes the contents. When you want to use another package, you first need to add it to your dependencies. This means running
yarn add [package-name] to install it into your project.
This will also update your
package.json and your
yarn.lock so that other developers working on the project will get the same dependencies as you when they run
You can specify versions using one of these:
yarn add package-nameinstalls the “latest” version of the package.
yarn add [email protected]installs a specific version of a package from the registry.
yarn add [email protected]installs a specific “tag” (e.g.
You can also specify packages from different locations:
yarn add package-nameinstalls the package from the npm registry unless you have specified another one in your
yarn add file:/path/to/local/folderinstalls a package that is on your local file system. This is useful to test out other packages of yours that haven’t been published to the registry.
yarn add file:/path/to/local/tarball.tgzinstalls a package from a gzipped tarball which could be used to share a package before publishing it.
yarn add <git remote url>installs a package from a remote git repository.
yarn add <git remote url>#<branch/commit/tag>installs a package from a remote git repository at specific git branch, git commit or git tag.
yarn add https://my-project.org/package.tgzinstalls a package from a remote gzipped tarball.
If you have used a package manager like npm previously, you may be looking for how to add global dependencies.
For the vast majority of packages it is considered a bad practice to have global dependencies because they are implicit. It is much better to add all of your dependencies locally so that they are explicit and anyone else using your project gets the same set of dependencies.
If you are trying to use a CLI tool that has a
bin you can access these in your
./node_modules/.bin directory. You can also use the
yarn global add <package...>
yarn add <package...>
This will install one or more packages in your
yarn add <package...> [--dev/-D]
-D will install one or more packages in your
yarn add <package...> [--peer/-P]
-P will install one or more packages in your
yarn add <package...> [--optional/-O]
-O will install one or more packages in your
yarn add <package...> [--exact/-E]
-E installs the packages as exact versions. The default is to use the most recent release with the same major version. For example,
yarn add [email protected] would accept version
yarn add [email protected] --exact would only accept version
yarn add <package...> [--tilde/-T]
-T installs the most recent release of the packages that have the same minor version. The default is to use the most recent release with the same major version. For example,
yarn add [email protected] --tilde would accept
1.2.9 but not
yarn add <package...> [--ignore-workspace-root-check/-W]
-W allows a package to be installed at the workspaces root. This tends not to be desired behaviour, as dependencies are generally expected to be part of a workspace. For example
yarn add lerna --ignore-workspace-root-check --dev at the workspaces root would allow lerna to be used within the scripts of the root package.json
yarn add <alias-package>@npm:<package>
This will install a package under a custom alias. Aliasing, allows multiple versions of the same dependency to be installed, each referenced via the alias-package name given. For example,
yarn add [email protected]:foo will install the package
foo (at the latest version) in your
dependencies under the specified alias
yarn add [email protected]:[email protected] allows a specific version of
foo to be installed.
yarn add <package...> --audit
Checks for known security issues with the installed packages. A count of found issues will be added to the output. Use the
yarn audit command for additional details.
© 2016–present Yarn Contributors
Licensed under the BSD License.