Yarn add
This command enables you to install a package, as well as the packages it depends on.
Adding dependencies
Generally, a package refers to a folder that contains code as well as a package.json file that describes its contents. Sometimes, you would want to use another package in your project, in that case you will need to add that package as part of your dependencies, to do this you will have to run yarn add [package-name] from your terminal.
When you run the command above, your package.json and yarn.lock files will be updated. This ensures that other developers that are working on the same project will have the same dependencies that you have when they run yarn or yarn install.
.Most packages are installed from the npm registry and can be referred to by simply calling their package name. for instance, running yarn install express> from your terminal, will install the express package from the registry.
From your command-line, there are three ways you can specify versions; they are:
- yarn add package-name this will install the "latest" version of the package.
- yarn add [email protected] this will install a specific version of a package from the registry.
- yarn add [email protected] this will install a specific "tag" (e.g. beta, next, or latest).
Installing a package from the npm registry is not the only way to install a package, here is a list of different locations you can install a package.
NOTE: the commands listed below are to be run from your command-line.
- yarn add package-name command will install the package from the npm registry unless you have specified another one in your package.json.
- yarn add file:/path/to/local/folder command installs a package that is on your local file system. This is useful to test out other packages of yours that have not been published to the registry.
- yarn add file:/path/to/local/tarball.tgz command installs a package from a gzipped tarball which could be used to share a package before publishing it.
- yarn add <git remote url> command installs a package from a remote git repository.
- yarn add <git remote url>#<branch/commit/tag> command installs a package from a remote git repository at specific git branch, git commit or git tag.
- yarn add https://my-project.org/package.tgz command installs a package from a remote gzipped tarball.
Caveats
For most packages, having global dependencies is considered a bad practice, because they are implicit. It is better to add all your dependencies explicitly so that they will be explicit and anyone that is using the project will have the same set of dependencies. If you are using a CLI tool that has a bin you can access it in your./node_modules/.bin
directory or you can run the global command from the command-line:yarn global add <package...>
Commands
This section will give you an explanation to all the yarn add options that you will run from the command line.
yarn add <package...>
This command will install one or more packages in your dependencies.
yarn add <package...> [--dev/-D]
Using the --dev or -D option, will install one or more packages in your devDependencies.
yarn add <package...> [--peer/-P]
Using the --peer or -P option will install one or more packages in your peerDependencies.
yarn add <package...> [--optional/-O]
Using the --optional or -O will install one or more packages in your optionalDependencies.
yarn add <package...> [--exact/-E]
Using the --exact or -E option installs the packages as exact versions. The default will be to use the most recent release with the same major version. For instance, yarn add [email protected]would accept version1.9.1, but yarn add [email protected] --exact would only accept version 1.2.3.
yarn add <package...> [--tilde/-T]
Using --tilde or -T option will install the most recent release of the packages that have the same minor version. The default will be to use the most recent release with the same major version. For instance, yarn add [email protected] --tilde would accept 1.2.9 but not 1.3.0.
yarn add <package...> [--ignore-workspace-root-check/-W]
When you use --ignore-workspace-root-check or -W it allows a package to be installed at the workspaces root. This is usually not to be desired behavior, as the dependencies are generally expected to be part of a workspace. For example yarn add request --ignore-workspace-root-check --dev at the workspaces root would allow request to be used within the scripts of the root package.json
yarn add <alias-package>@npm:<package>
The alias option will install a package under a custom alias. Aliasing, enables multiple versions of the same dependency to be installed, each of them referenced via the alias-package name given. For example, yarn add [email protected]:foo will install the package axios (at the latest version) in your dependencies under the specified alias my-axios. Also, yarn add [email protected]:[email protected] allows a specific version of axios to be installed.
yarn add <package...> --audit
This will check for known security issues with the installed packages. A count of found issues is added to the output.
Previous:
Yarn cli commands
Next:
Yarn audit and autoclean commands
- New Content published on w3resource :
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework