How to install specific version of npm package?

MoreOnFew
Posted underJavascript

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 react@17.0.1
npm install specific version
Example showing npm command to install specific version of package

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
npm install latest version
Example showing npm command to install the latest version of a package from the npm repository.

Please note that both these commands can be used along with the --save and --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
Exampe showing npm view command
Example showing the npm view command

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
npm command to get all versions of package
Example showing the npm command to get all the version numbers of a package

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.

TaggedJavascriptNodeJsnpmpackagejs


MoreOnFew.com – Read tutorials related to JavaScript, jQuery, HTML5, CSS3 and more !

Cover Image for How to check if checkbox is checked in jQuery?

How to check if checkbox is checked in jQuery?

It is very easy to check if a checkbox is checked in jQuery using the .is() or .prop() method of jQuery combined with the ‘:checked’ selector. There are other ways too like checking for the attribute checked value, however, they aren’t very reliable. The easiest and most stable way to check if checkbox is checked … Read more

MoreOnFew
Cover Image for [Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

[Solved] Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’

You might be facing an “Attempted import error: ‘Switch’ is not exported from ‘react-router-dom’ ” error if you are using the react-router-dom package version 6. This error is caused due to using the older switch syntax of the react-router-dom. From version 6 onwards, the react-router-dom has replaced “Switch” with “Routes”. Let us take a look … Read more

MoreOnFew