Managing multiple NodeJS versions with nvm
nvm
is a version manager for node.js, designed to be installed per-user, and invoked per-shell. nvm
works on any POSIX-compliant shell (sh, dash, ksh, zsh, bash), in particular on these platforms: UNIX, macOS, and Windows WSL.
To install the latest version of Node.js, run the following command:
nvm install node
Sample output:
Downloading and installing node v16.9.1... Downloading https://nodejs.org/dist/v16.9.1/node-v16.9.1-linux-x64.tar.xz... ################################################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v16.9.1 (npm v7.21.1) Creating default alias: default -> node (-> v16.9.1)
To install the latest stable version of Node.js, run the following command:
nvm install --lts
Sample output:
Installing latest LTS version. Downloading and installing node v14.17.6... Downloading https://nodejs.org/dist/v14.17.6/node-v14.17.6-linux-x64.tar.xz... ############################################################################################################################ 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.17.6 (npm v6.14.15)
To install the specific Node.js version like v14.15.4, run the following command:
nvm install v14.15.4
Sample output:
Downloading and installing node v14.15.4... Downloading https://nodejs.org/dist/v14.15.4/node-v14.15.4-linux-x64.tar.xz... ############################################################################################################################# 100.0% Computing checksum with sha256sum Checksums matched! Now using node v14.15.4 (npm v6.14.10)
Switch Between Node.js Versions
To verify the active Node.js version, run the following command:
node --version
Sample output:
v14.15.4
If you want to switch to the latest Node.js version, run the following command:
nvm use node
Sample output:
Now using node v16.9.1 (npm v7.21.1)
To switch to the latest LTS version, run the following command:
nvm use --lts
Sample output:
Now using node v14.17.6 (npm v6.14.15)
If you want to run a command directly for an installed version without switching the node variable, run:
nvm run default --version
Sample output:
Running node v16.9.1 (npm v7.21.1) v16.9.1
List Node.js Versions
If you want to list all installed Node.js versions, run the following command:
nvm ls
Sample output:
v14.15.4 -> v14.17.6 v16.9.1 default -> node (-> v16.9.1) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v16.9.1) (default) stable -> 16.9 (-> v16.9.1) (default)
To display the current Node.js version, run the following command:
nvm current
Sample output:
v14.17.6
To list all available Node.js versions, run the following command:
nvm ls-remote
You should get the following output:
-> v14.17.6 (Latest LTS: Fermium) v15.0.0 v15.0.1 v15.1.0 v15.2.0 v15.2.1 v15.3.0 v15.4.0 v15.5.0 v15.5.1 v15.6.0 v15.7.0 v15.8.0 v15.9.0 v15.10.0 v15.11.0 v15.12.0 v15.13.0 v15.14.0 v16.0.0 v16.1.0 v16.2.0 v16.3.0 v16.4.0 v16.4.1 v16.4.2 v16.5.0 v16.6.0 v16.6.1 v16.6.2 v16.7.0 v16.8.0 v16.9.0 v16.9.1
Run Application with Specific Version
If you have multiple applications on your system and want to run each application with a specific version of node.js then you can use the option to use a node.js version for any application.
For example, to run app.js with Node.js version v14.15.4, run the following command:
nvm run v14.15.4 app.js
You can also use exec to run a command on a sub-shell:
nvm exec 14.15.4 node --version
Sample output:
Running node v14.15.4 (npm v6.14.10) v14.15.4
To find the path of the Node.js executable of a specific version, run the following command:
nvm which 14.15.4
Sample output:
/root/.nvm/versions/node/v14.15.4/bin/node
Remove Node.js Version
First, list all installed Node.js versions using the following command:
nvm list
Next, remove any unused version from your system using the following command:
nvm uninstall 14.15.4
Sample output:
Uninstalled node v14.15.4