To install a specific version of the Node.js package,  NPM is the default package management utility for Node.js packages or modules. You can use it to install and manage versions of dependencies in your projects.

NPM stands for:-  Node Package Manager

It’s possible to use npm to install a specific version of a package and save your project from breaking due to introducing incompatible updates.

This blog talks about how to install a particular Node.js package version via npm. It covers the following topics:

Contents

1 How To Install A Specific Version Of A Package
2 How To Know Package Versions Available
3 How To Know The Specific Version of An Installed Package
4 How To Install An Older Version Of An NPM Package
5 How To Use Semantic Versioning To Specify Install Versions
6 Summary


1. How To Install A Specific Version Of A Package

You can use the npm install command to download and install a package on your development environment.

Here is an example:


By default, the npm install command fetches the latest available version of the specified package—in this case, it’s Cypress version 9.1.1, as of the time of this writing.

However, what if the latest version causes some breaking changes to your application, or you just need a different version for any other reason?

In such cases, installing an exact package version could best suit your needs. You can simply specify the version using the @ syntax.

Here’s the command to run for npm install a specific version:

npm install [package-name]@[version-number]

The above command will install the particular package version you want, alongside any packages that it depends on.

For example, to install a specific version of Cypress, you can run the following command:

npm install cypress@8.7.0

Or, you can use the shortened version of the command:

npm i cypress@8.7.0

The above command will install Cypress version 8.7.0 locally.

Here is an example:


If you want to install it globally, you can simply add the -g (short for –global) flag:

npm install -g cypress@8.7.0


2. How To Know Package Versions Available

If you want to know the exact version of a package to install, you can simply search for it on the npm public registry database.

Or, you can simply run the following command to check the available versions on the npm registry:

npm view [package-name] versions

If you want to know the specific latest version of a package available on the npm registry, run the following command:

npm view [package-name] version

For example, here is how you can check the latest version of the Cypress package:



3. How To Know The Specific Version of An Installed Package

If you want to see a tree-structured list of all your locally installed packages, including their dependencies, run the following command:

npm list

Or, its shortened version:

npm ls

Here is an example:


Of course, you can just go to the package-lock.json file, but manually checking the packages’ versions will involve some visual scanning.  

If you want to display all installed top-level packages without their dependencies, add the –depth=0 flag. The flag is used to limit the depth of the dependency tree that can be displayed.

Here is an example:


If you want to check globally installed packages, just add the -g (short for –global) flag:

npm list -g

If you want to know the specific latest version of an installed package, just specify its name.

npm list [package-name]

Here is an example:

4. How To Install An Older Version Of An NPM Package

You may want to install an older version of an npm package for any reason, such as to resolve compatibility issues or bugs.

To downgrade to a particular older version, just specify it using the @ syntax. It’s the same process as installing a specific version of a package, as was described previously.

Here is the syntax:

npm install [package-name]@[version-number]


5. How To Use Semantic Versioning To Specify Install Versions

Semantic Versioning Specification (SemVer) is a set of convention rules that npm follows to stipulate how packages are versioned.

Syntax: The syntax of the npm version looks like the following.

Major.Minor.Patch

Every package version has three numbers, such as 8.7.0, representing major.minor.patch versions, respectively.

npm allows you to use SemVer to specify the package version to install. You can use a caret (^) character to specify the latest minor version to install or a tilde (~) character to specify the latest patch version to install.

For example, if you do not know the specific minor version of the package to install, you can prefix the version number with a caret:



The above command fetched the highest minor version of the package, under 9.x.x., which turned out to be 9.1.1.

Also, if you want to keep your packages up-to-date with the latest security patches, but you do not know the latest version in that range, you can prefix the version number with a tilde:


The above command fetched the latest patched version of the package, under 9.1.x., which turned out to be 9.1.1.


6. Summary

  • For npm install specific version, use npm install [package-name]@[version-number].
  • Use npm view [package-name] version to know the specific latest version of a package available on the npm registry.
  • Use npm list [package-name] to know the specific latest version of an installed package.
  • Use npm install [package-name]@[version-number] to install an older version of a package.
  • Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.