How to install a previous exact version of a NPM package?

Better Stack Team
Updated on March 11, 2024

To install a specific version of an npm package, you can use the npm install command along with the package name and the desired version number. Here's the syntax:

 
npm install <package_name>@<version_number>

Replace <package_name> with the name of the package and <version_number> with the specific version you want to install.

For example, to install version 1.2.3 of a package named "example-package," you would run:

 
npm install example-package@1.2.3

Additionally, you can use other version specifiers, such as tilde (~) or caret (^), to specify version ranges. For example:

 
npm install example-package@^1.2.0

This would install the latest version compatible with version 1.2.0.

If you want to install a specific version globally, you can use the -g flag:

 
npm install -g example-package@1.2.3

Keep in mind that specifying exact versions may lead to potential issues in the long term, as you won't automatically receive updates. It's generally a good practice to use version ranges or semver specifiers when possible.