How to install a specific version of an npm package

Posted on May 24, 2022

Learn how you can install a specific version of a package using npm

By default, npm will install the latest stable version of a package when you run the npm install command.

To install a specific version of a package, you need to state the version you want to install using the @ symbol.

The format is as follows:

npm install [package name]@[major.minor.patch]

Since npm uses semantic versioning, a package version is specified using the major.minor.patch version format.

For example, suppose you want to install react version 15.5.0.

Here’s how you do it:

npm install react@15.5.0

The command above will fetch the react package with the exact version you stated using the @x.x.x format.

You can also select the latest major version of a package by stating only the major version as follows:

npm install react@15

The command above will install react version 15 with the latest minor and patch version, which is 15.7.0.

You can also specify the major and minor versions but without the patch version:

npm install react@15.5

The latest patch of react version 15.5 is 15.5.4 so that’s the version that will be installed.

To install a specific version of a package globally, just add the -g option after the install command:

npm install -g gulp@3

The command above will install version 3 of the gulp package globally.

You can also see all versions available for installation using the npm view [package-name] versions command.

Here’s an example of seeing available gulp versions:

$ npm view gulp versions
  '0.0.1',  '0.0.2', '0.0.3', '0.0.4', '0.0.5',
  '0.0.7',  '0.0.8', '0.0.9', '0.1.0', '0.2.0',
  '1.0.0',  '1.1.0', '1.2.0', '1.2.1', '2.0.0',
  '2.0.1',  '2.1.0', '2.2.0', '2.3.0', '2.4.0',
  '2.4.1',  '2.6.0', '2.6.1', '2.7.0', '3.0.0',
  '3.1.1',  '3.1.2', '3.1.3', '3.1.4', '3.2.0',
  '3.2.1',  '3.2.2', '3.2.3', '3.2.4', '3.2.5',
  '3.3.0',  '3.3.1', '3.3.2', '3.3.4', '3.4.0',
  '3.5.0',  '3.5.1', '3.5.2', '3.5.5', '3.5.6',
  '3.6.0',  '3.6.1', '3.6.2', '3.7.0', '3.8.0',
  '3.8.1',  '3.8.2', '3.8.3', '3.8.4', '3.8.5',
  '3.8.6',  '3.8.7', '3.8.8', '3.8.9', '3.8.10',
  '3.8.11', '3.9.0', '3.9.1', '4.0.0', '4.0.1',

You can define the specific version you want to install based on the output above.

Installing next builds

Additionally, you can also use @next to install the next version of a package.

The next version is commonly used to tag an experimental package build that’s not yet released to the public. This version is usually unstable and may contain bugs.

Since npm packages are open-sourced, the developers rely on the community to provide feedback and report bugs.

Generally, you can install the latest unstable version using the following command:

npm install react@next

Please keep in mind that you are not recommended to use the next version for the production environment. The purpose of this version is for testing.

Now you’ve learned how to install a specific version of an npm package.

Installing a specific version of a package is useful when you need to roll back a package update because of breaking changes in the latest version.

You can also use it to help package developers test the latest nightly builds before it was released to the public.

Level up your programming skills

I'm sending out an occasional email with the latest programming tutorials. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.