How to change NPM version in Linux, Windows or macOS?

Working on different Node.js projects and the version of dependencies required by them; you may often need to switch between different versions of the Node Package Manager (NPM). However, for beginners, it can be a tricky task but don’t worry. In this tutorial, we will discuss simple steps to change the NPM version with ease. So, that anybody could perform them.

Before we begin, let us first understand what NPM is and why version switching is necessary. NPM is a package manager for Node.js, which allows developers to easily manage and install packages and dependencies for their projects. However, some projects may require a specific version of NPM, and that is where version switching comes into play.

Now, let’s go through the key steps to change the NPM version:

Step 1: Check the current NPM version

You should know the current version of NPM installed on your system before issuing any command to change it. To do that on your PC – Windows, Linux, or macOS; open the command terminal, and after that run the given command.  

npm -v

The above command will display the current NPM version installed on your system.

Step 2: Install a new version of NPM 

If you want to have some other version of NPM than the one currently installed on your system then you need to install it manually, here is the command syntax to do that. 

sudo npm install -g npm@version_number

In the above-given syntax, replace the ‘version_number‘ text with the exact version of NPM you want to install on your system.

For example, our current version is 9.5.0 but we need 6.14.5 for our project then to install it, we can use the “NPM Install” command in this way:

npm install -g npm@6.14.5

Note: -g stands for global, if you want to install the new version only for your current project then switch to its directory and run the command but without -g 

Step 3: Verify the new NPM version

After successfully installing the NPM required version, we can check whether it has been set as the default current version or not. For that run:

npm -v

This will display the newly installed version of NPM.

Step 4: Switch back to the previous NPM version

You can see in the previous step we have successfully changed the current NPM version to what want. Now, if we want to get back the previous version that we have changed, we need to install it using the same command.

For example, we switch to NPM 6.14.5 and now want to go back to 9.5.0, then we use:

 npm install -g [email protected]
Command to change NPM version

NVM Usage to switch Node version.

Those who want to switch the Node version and use the corresponding version of NPM available for it can use the NVM (Node Version Manager) tool that facilitates us with a command to switch between different versions of Node.js and NPM. 

However, by default NVM will not be on your system, to install it run:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash

After that:

source ~/.bashrc

Let’s check first our system Node version:

node -v

Now, you can list available LTS versions to use with NVM

nvm ls

The versions in the red color of Node are not on your system, so if you want to use any of them on your system then you first need to install the same.

Note: To list all the Node versions even those are not compliant with LTS, the command will be nvm ls-remote

For that, the command is:

nvm install node-version

For example, to get the node version – 16.20.0, 

nvm install 16.20.0

Once you have installed multiple versions of Node.js you can use the following command to switch between them.

the command syntax is:

nvm use version_number

Here, ‘version_number‘ refers to the version of the Node version you want to use. For example, if you want to switch to Node version 16.20.0 from the current one then the command will be:

nvm use 16.20.0

This will switch not only the Node but also the NPM version as well.

Change node version using NVM command

Conclusion

Changing NPM and Nodejs versions would be seem difficult to do at first sight but as you get familiar with its command line and NVM, it can be done easily. By following these steps, you can install a new version of NPM, verify the installation, and switch between different versions using NVM. This helps you to work on projects that require specific versions of NPM.

Remember, always check the compatibility of NPM versions before installing or switching to avoid any issues. We hope this article has been helpful to you.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.