As you might already know, there are a large number of npm packages released every day. An equally large number of updates too get released on the npm registry. While we would like to keep the dependencies in package.json updated to the latest version most of the time, there are still times when we would like to install specific version of npm package for certain reasons.
How to install an exact or specific version of npm package?
In order to install a specific version of npm package run the npm install command along with the package name followed by the version number prefixed with the at symbol “@”. Here is the syntax for the same:
npm install <package_name>@version_number
For example, let’s say I wanted to install React version 17.0.1, then the command would be :
npm install [email protected]
If you notice, the command is very similar to the basic command to install a package, however, the only difference is the @version_number that follows the package name as a suffix.
How to install the latest version of a package in npm?
On the other hand, if you would like to install the latest version of an npm package, use the same command but instead of specifying the version number, use the “latest” keyword like:
npm install <package_name>@latest
For example, to install the latest version of React, the command would be
npm install react@latest
Please note that both these commands can be used along with the
--save-dev etc flags too whenever required.
Alternatively, you can also use the npm view command to get the latest version number of the package and pass that version number
How to get the latest npm package version number available on npm registry?
While you can use the npm install <package_name>@latest command to install the latest version, there is another command (npm view) that can be used to get the version number of the latest package available. You can do so by running the following command:
npm view <package_name> version
For example, say you wanted to know which is the latest version of ReactJs available as per the npm registry, you can run the following command
npm view react version
How to get all versions of package available on npm registry?
On the other hand, if you would like to list all the versions of a package available on the npm registry, then you can run the following command
npm view <package_name> versions
If you notice, the only difference between the previous two commands is the word “version” being plural. So when you use the “version” keyword, it would show the latest version number. However, when you use the “versions” (plural) keyword, you get the list of all the version numbers of the package as per the npm registry.
For example, to get all the versions of the jquery package available in the npm registry, we can run the following command to see the list of all the version numbers of jquery as per the npm registry.
npm view jquery versions
As you can see, it is quite simple to install specific version of npm package or to install the latest version of a package too. You can use npm commands to find and install the latest version of a package available too. You can also use npm commands to find outdated packages or to find unused dependencies in npm and to remove those npm packages too in order to keep your project optimized. Hope you found this article helpful.