The short answer
To check the currently installed version number of a package, you can use the following yarn list command:
$ yarn list --pattern “package”
Run in Warp
Where the --pattern flag is used to filter the list of packages by pattern.
For example:
$ yarn list --pattern “express”
└─ [email protected]
Run in Warp
To update a package to its latest version available on the npm registry, you can use the yarn upgrade command combined with the --latest flag as follows:
$ yarn upgrade --latest <package>
Run in Warp
Note that when using the yarn upgrade command, the package.json and yarn.lock files will automatically be updated to reflect the changes made to the dependencies and sub-dependencies of the project.
If you want to update the yarn command-line tool itself, you can read our article on how to update yarn globally.
Easily retrieve this command using Warp’s AI Command Search
If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:
Entering yarn update package latest version in the AI Command Search will prompt a yarn command that can then quickly be inserted into your shell by doing CMD+ENTER.
Updating a package to a specific version
To update a package to a specific version, you can use the aforementioned yarn upgrade command and append the desired version number (i.e. 1.2.3) or distribution tag (i.e. latest) to the package name using the following syntax:
$ yarn upgrade <package>@<version|tag>
Run in Warp
For example:
$ yarn upgrade [email protected]
$ yarn upgrade express@next
Run in Warp
Updating multiple packages at once
To update all the packages of your project to their latest version based on the version range specified in the package.jsonfile, you can use the yarn upgrade command as follows:
$ yarn upgrade
Run in Warp
Alternatively, to update one or more packages, you can list them as arguments of the `yarn upgrade` command as follows:
$ yarn upgrade <package …>
Run in Warp
For example:
$ yarn upgrade express axios
Run in Warp
Updating packages to the latest tag version
To update one, several, or all the packages of your project to the version matched by the latest tag available on the npm registry, regardless of the version range specified in the package.json file, you can use a combination of the aforementioned yarn upgrade command and the --latest option flag as follows:
$ yarn upgrade --latest [package …]
Run in Warp
For example, this command will update all the packages:
$ yarn upgrade --latest
Run in Warp
And this command will only update the express and axios packages:
$ yarn upgrade --latest express axios
Run in Warp
Updating sub-dependencies
When updating a top-level package with the yarn update command, its sub-dependencies will automatically be updated in order to ensure compatibility between versions.
However, in some cases, you may want to independently update a sub-dependency without updating the top-level package that relies on it, for example, due to a security fix.
To do so, you can override its current version in the resolutions fields of the package.json file as follows:
{
"dependencies": {
"express": 4.3.2
},
"resolutions": {
"body-parser": "2.3.4"
}
}
Run in Warp
Installing a package to a specific version
By default, the yarn add command will install the latest available version of a package. To install a specific version instead, you can use the following syntax:
$ $ yarn add <package>@<version|tag>
Run in Warp
Where version is a version number (e.g. 1.2.3) and tag is a distribution tag (e.g. latest).
For example:
$ yarn add [email protected]
$ yarn add express@next
Run in Warp
Written by
Razvan Ludosanu
Founder, learnbackend.dev
Filed Under