How to Install and Switch Between Multiple Node.js Versions
Node.js is an open-source, cross-platform JavaScript run-time environment for running JavaScript code outside of a web browser.
How to Change The Global Modules Directory of Node.js in Ubuntu
When working on a Node.js project, at some point, you would want to be able to switch between multiple versions of Node.js. Maybe you're upgrading an old Node.js project to a newer version. Unfortunately, Node.js does not offer us any options to work with multiple versions of it. For this, we will need to become familiar with a very handy tool called NVM (Node Virtual Manager). It will allow us to easily switch between Node.js versions.
INSTALLING NVM
We can install nvm using either curl
or wget
.
With curl
,
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
With wget
,
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
v0.37.2
is the latest version of nvm available at the time of writing this. The version number will change as the project develops. So, to make sure you install the latest version, it's best to visit the project's Github page.
After installing, to get the nvm
command available on the terminal, run the following command or close your current terminal instance and open a new one.
source ~/.bashrc
Run the following command to confirm that we can use the nvm
command.
nvm --version
0.37.2
USING NVM
Now that we have successfully installed nvm, let us look at some basic operations.
1. Get List of Node.js Versions
For seeing the list of Node.js versions available to us, run the following command.
nvm ls-remote
2. Installing Versions of Node.js
We can install a specific version of Node.js using nvm install
as shown below.
nvm install 12.20.1
The above command will install version 12.20.1
of Node.js
When we
nvm install
a version, it will become our active version of Node.js.
Since nvm follows Semantic Versioning, for installing the latest 12.18
patch, run the following command.
nvm install 12.18
The above command will install 12.18.x
where x
is the latest patch number available. At the time of writing, for version 12.18
, the latest patch number is 4
. So, the above command will install 12.18.4
.
Run the following command to install the latest version of Node.js. At the time of writing, it is version 15.6.0
.
nvm install node
Run the following command to install the latest LTS version of Node.js. At the time of writing, it is version 14.15.4
.
nvm install --lts
3. Uninstalling Versions of Node.js
For uninstalling a specific version of Node.js, we can use the nvm uninstall
command,
nvm uninstall 12.20.1
4. Check Currently Using Node.js Version
We can check what version of Node.js we are currently using with either the nvm
or node
commands.
nvm current
node -v
5. List Installed Versions
Use the nvm ls
to see all the Node.js versions we installed.
nvm ls
v12.18.4 v12.20.1 -> v14.15.4 v15.6.0 system default -> lts/* (-> v14.15.4) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v15.6.0) (default) stable -> 15.6 (-> v15.6.0) (default) lts/* -> lts/fermium (-> v14.15.4) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.23.1 (-> N/A) lts/erbium -> v12.20.1 lts/fermium -> v14.15.4
6. Use a Specific Version
To switch to a specific version which we have installed, use the nvm use
command.
If it is installed, use the following command to switch to the latest version of Node.js.
nvm use node
if it is installed, use the following command to switch to the latest LTS version of Node.js.
nvm use --lts
If a specific version of Node.js is installed, to switch to it, use the following command.
nvm use 12.18.4
UNINSTALLING NVM
If for some reason you wish to uninstall nvm, all we have to do is delete the ~/.nvm
directory.
rm -r ~/.nvm