npm: How To Install A Specific Version of Node.js Package

Using NPM To Install A Specific Version Of A Node.js Package

The Node Package Manager (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.

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:

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:

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

By default, the npm install command fetches the latest available version of the specified package—in this case, it’s Renovate version 24.52.1, as of the time of this writing. Renovate is a resourceful package that allows you to automate your dependency updates. 

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 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 Renovate, you can run the following command:

  npm install renovate@20.5.1

 

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

  npm i renovate@20.5.1

 

The above command will install Renovate version 20.5.1 locally. 

Here is an example:

install Renovate version 20.5.1 locally

 

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

  npm install -g renovate@20.5.1

 

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 Renovate package:

check the latest version of the Renovate package

 

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:

a tree-structured list of all your locally installed packages, including their dependencies

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:

display all installed top-level packages without their dependencies

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:

know the specific latest version of an installed package

 

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]

 

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. Every package version has three numbers, such as 24.42.1, 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:

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 20.x.x., which turned out to be 20.25.8. 

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:

prefix the version number with a tilde

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

 

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. 

 

Updating versions of Node.js packages manually? There’s an easier way

 

Renovate is an open source tool by Mend for developers and DevOps that automatically creates pull requests (PRs) for dependency updates. Renovate PRs embed all the information you need to ease your update decision.

 

Renovate can upgrade the Node.js runtime and packages used by your project. This way you have access to the latest features, bug fixes, performance improvements, and security patches.

Alfrick Opidi / About Author

Alfrick is an experienced full-stack web developer with a deep interest in taking technical information and converting it into easy to understand content. See him as a technology enthusiast who explores the latest developments in the industry and presents them in a relatable, concise, and decipherable manner.

LinkedIn