Managing dependencies and upgrading dependencies with Yarn
Managing dependencies
As shown in the last tutorial we introduced you to the yarn init command used to initialize a package.json file for your project. The package.json file which has been described in the previous tutorial has a dependencies field that indicates the packages that your projects need to run.
In this tutorial we will show you how to add, upgrade or remove dependencies. To do any of these, there are certain commands that you need to know. Any of these commands will automatically update your package.json file as well as your yarn.lock file.
Adding a dependency
For you to use another package in your project, you will need to add the package as a dependency. To do this, you will have to run this from the command line:
```yarn add [package]```
This command will add the [package] to your dependencies in your package.json. The command will also update your yarn.lock to reflect the change.
```{
"name": "my-package",
"dependencies": {
+ "package-1": "^1.0.0"
}
}```
There are other types of dependencies that you can add to your project using their respective flags:
- yarn add --dev you should run this command to add to the devDependencies
- yarn add --optional this is used to add to optionalDependencies
- yarn add --peer when you run this command it adds to peerDependencies
You can also specify the version of a package that you want to install, all you need to do is to specify either the dependency version or a tag.
```yarn add [package]@[version]
yarn add [package]@[tag]```
The [version] or [tag] will be what gets added to your package.json and will then be resolved against when installing the dependency.
Here is an example:
```
yarn add [email protected]
yarn add package-2@^1.0.0
yarn add package-3@beta
```
```{
"dependencies": {
"package-1": "1.2.3",
"package-2": "^1.0.0",
"package-3": "beta"
}
}```
Upgrading a dependency
Let us take a case scenario where a package that you depend on has been updated or you are working with a latest version that has some issues and you want to switch to a stable version. What you need to do is to update your dependency. To just update your package to the latest version, run:
yarn upgrade [package]
where as to go to a particular version or tag of the package, you can run these commands to move to a version or tag respectively:
```yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]```
This commands will upgrade both your package.json as well as the yarn.lock file.
```
{
"name": "my-package",
"dependencies": {
- "package-1": "^1.0.0"
+ "package-1": "^2.0.0"
}
}```
Removing a dependency
There are times when you no longer need a package in your project, to remove the package you will need to run:
yarn remove [package]
This command will remove the package and then update your package.json file as well as your yarn.lock file.
Previous:
Installing Dependencies and working with version control.
Next:
Using Yarn for continuous Integration across different CI systems.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/yarn/managing-and-upgrading-dependencies.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics