Terminus
Upgrade Yarn Package(s)

Upgrade Yarn Package(s)

The short answer

To check the currently installed version number of a package, you can use the following [.inline-code]yarn list[.inline-code] command:

$ yarn list --pattern “package”

Where the [.inline-code]--pattern[.inline-code] flag is used to filter the list of packages by pattern.

For example:

 $ yarn list --pattern “express”
└─ express@4.18.2

To update a package to its latest version available on the npm registry, you can use the [.inline-code]yarn upgrade[.inline-code] command combined with the [.inline-code]--latest[.inline-code] flag as follows:

 $ yarn upgrade --latest <package>

Note that when using the [.inline-code]yarn upgrade[.inline-code] command, the [.inline-code]package.json[.inline-code] and [.inline-code]yarn.lock[.inline-code] 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 [.inline-code]yarn[.inline-code] command-line tool itself, you can read our article on how to update [.inline-code]yarn[.inline-code] globally.

[#easily-recall-with-ai] Easily retrieve this command using Warp’s AI Command Search [#easily-recall-with-ai]

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Search feature:

Entering [.inline-code]yarn update package latest version[.inline-code] in the AI Command Search will prompt a [.inline-code]yarn[.inline-code] command that can then quickly be inserted into your shell by doing [.inline-code]CMD+ENTER[.inline-code].

[#update-to-specific-version]Updating a package to a specific version  [#update-to-specific-version]

To update a package to a specific version, you can use the aforementioned [.inline-code]yarn upgrade[.inline-code] command and append the desired version number (i.e. [.inline-code]1.2.3[.inline-code]) or distribution tag (i.e. [.inline-code]latest[.inline-code]) to the package name using the following syntax:

$ yarn upgrade <package>@<version|tag>

For example:

$ yarn upgrade express@4.18.2
$ yarn upgrade express@next

[#update-based-on-package-json]Updating multiple packages at once [#update-based-on-package-json]

To update all the packages of your project to their latest version based on the version range specified in the [.inline-code]package.json[.inline-code]file, you can use the [.inline-code]yarn upgrade[.inline-code] command as follows:

 $ yarn upgrade

Alternatively, to update one or more packages, you can list them as arguments of the `yarn upgrade` command as follows:

$ yarn upgrade <package …>

For example:

$ yarn upgrade express axios

[#update-to-latest-version] Updating packages to the [.inline-code]latest[.inline-code] tag version[#update-to-latest-version]

To update one, several, or all the packages of your project to the version matched by the [.inline-code]latest[.inline-code] tag available on the npm registry, regardless of the version range specified in the [.inline-code]package.json[.inline-code] file, you can use a combination of the aforementioned [.inline-code]yarn upgrade[.inline-code] command and the  [.inline-code]--latest[.inline-code] option flag as follows:

$ yarn upgrade --latest [package …]

For example, this command will update all the packages:

$ yarn upgrade --latest

And this command will only update the [.inline-code]express[.inline-code] and [.inline-code]axios[.inline-code] packages:

$ yarn upgrade --latest express axios

[#updating-sub-dependency] Updating sub-dependencies [#updating-sub-dependency]

When updating a top-level package with the [.inline-code]yarn update[.inline-code] 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 [.inline-code]resolutions[.inline-code] fields of the [.inline-code]package.json[.inline-code] file as follows:

{
  "dependencies": {
    "express": 4.3.2
  },
  "resolutions": {
    "body-parser": "2.3.4"
  }
}

[#install-specific-version] Installing a package to a specific version [#install-specific-version]

By default, the [.inline-code]yarn add[.inline-code] 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>

Where [.inline-code]version[.inline-code] is a version number (e.g. [.inline-code]1.2.3[.inline-code]) and [.inline-code]tag[.inline-code] is a distribution tag (e.g. [.inline-code]latest[.inline-code]).

For example:

$ yarn add express@4.18.2
$ yarn add express@next