Manage Node.js Version by NVM

Consider that you have multiple NVM projects. One of them dependents on node v14, and one of them dependents on node v16. How to switch different versions of nodes?

What is NVM?

NVM, Node Version Manager, allows you to quickly install and use different versions of Node via the command line. You can see the NVM project on https://github.com/nvm-sh/nvm

Install NVM on Linux

It is not difficult to install nvm on Linux. You can see instructions on https://github.com/nvm-sh/nvm#install--update-script.

At the beginning, download NVM and run the shell script by curl or wget.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

The above script will set environment variables automatically in ~/.bashrc. However, we should restart the terminal to apply new environment variables or instead use source ~/.bashrc.

source ~/.bashrc

If you do not know what is barshrc, I found some pages that introduces bashrc.

  1. English: https://www.journaldev.com/41479/bashrc-file-in-linux
  2. Chinese: https://www.itread01.com/content/1542833526.html

Install NVM on Windows

NVM does not supports Windows but there is a similar tool called nvm-windows .

  1. Download installation executable file. https://github.com/coreybutler/nvm-windows/releases
  2. Install.
  3. Restart command line.
  4. After installation. Type nvm version to check. If command line still can not find the path of nvm, please set environment variable manually .
See NVM’s version.

In my opinion, do not install NVM by chocolatey directly, because the version of nvm-windows on chocolatey is out of date. The new version of node.js is not supported by the older nvm-windows.

Install Node by NVM.

Instead of installing different NVM by yum or apt-install , in my experience, I recommend that install Node.js by NVM.

For example, if you want to install Node.js v16.14.2

nvm install 16.14.2

Besides, you can list all versions of Node.js on your environment by nvm list

nvm list
nvm list on windows 11
nvm list on Ubuntu 18

Switch difference versions of Node by NVM

After listing all the versions of Nodes. You can use one of them by nvm use

For example, if we have 3 versions of nodes

  1. Node v17.5.0
  2. Node v16.6.1
  3. Node v14.17.13

We can switch to v16.6.1 by nvm use 16.6.1 or just nvm use 16

Notice that I installed gsudo on my Windows. If you do not install gsudo in advance, please run the command on the administrative mode.

Reference

  1. https://editor.leonh.space/2021/nvm/
  2. https://www.onejar99.com/nvm-install-for-windows/
  3. https://community.chocolatey.org/packages/nvm

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kashiwa

Kashiwa

A person who loves computer science shares implementations. Github: @liao2000