How to Use Multiple Versions of Node.js?

Using Node Version Manager (NVM), you can install and use multiple versions of Node.js on your local machine.

Installing Multiple Versions of Node.js

Once you have NVM installed on your machine, you can install multiple versions of Node.js simply by using the nvm install command followed by the Node.js version. For example, the following will install Node.js v10 and v14 on your local machine:

nvm install 10
nvm install 14

NVM also allows you to install a specific Node.js version. For example, to install Node.js v12.22.1 you would run the following command:

nvm install 12.22.1

Listing All Installed Versions of Node.js

To list all versions of Node.js installed via NVM, you can run the nvm ls command.

Checking the Active Version of Node.js

You can simply run the node --version command to check which version of Node.js is currently active (i.e. being used currently).

Setting the Default Node.js Version

Setting a default version of Node.js would make that version available in shell everytime you open it. For example, to set v10 as default you could do the following:

nvm alias default 10

You could also specify a specific version. For example:

nvm alias default 10.24.1

Switching Between Node.js Versions

You can use the nvm use command followed by the Node.js version, to make that version active. For example:

nvm use 10

You may also switch back to the default version, for example, like so:

nvm use default

You may use the Node.js version installed on your system (instead of the one provided by NVM) in the following way:

nvm use system

Managing Projects That Use Different Node.js Versions

If you have multiple projects that use different versions of node, it may help to create a .nvmrc file in your project's root folder, for example, in the following way:

echo "10" > .nvmrc

This would allow you to run commands like nvm use, nvm install, etc. without specifying the version number as that would be picked up from the .nvmrc file. Additionally, you may also create a shell script that automatically switches between Node.js versions.

Uninstalling a Node.js Version

You can uninstall a Node.js version by specifying the version number following the nvm uninstall command. For example:

nvm uninstall 10

This would only uninstall the the latest installed version corresponding to the version number provided. This means that if you have several specific versions of Node.js installed, you will have to uninstall them one by one.

Please note that you can't uninstall a Node.js version that is currently active.


This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.