Easy Way to Install Multiple Version of Node JS

Basically, installing Node JS on your machine is very easy.

If you are using mac or windows, you can just download the package from the official website, then install it. Alternatively, you can install it via Homebrew for mac.

If you are using Linux, just install it via the package manager of your Linux distro, for example, use pacman for Arch Linux, and use apt-get for Debian-based. When your installation process is successful, you will have one version of Node js.

In some cases, you need multiple versions of Node JS on your computer. Especially if you work with multiple projects that require a different version of Node JS. Another reason that might make you want to install multiple versions is that you might want to use the LTS version for your current project but you also want to try new features of the latest version of Node JS. The best solution is to install two or more versions of Node JS on your computer. In case you are under the same circumstances, this post is right for you.

In this post, I will show you how to install multiple Node JS on your computer with the help of NVS (Node Version Switcher). NVS is a cross-platform utility for switching between different versions of Node JS. You can easily install and switch to a different version of Node JS anytime you want.

NVS Installation process only takes less than a minute. Below are the steps to install it.

Install NVS on Windows

There are two ways to install nvs on your windows computer. You don't need to do both, you can choose one of them.

  1. Download the NVS msi file here (windows installer), then install it the same way as you install other windows applications.
  2. Use chocolatey (choco). Make sure you already have choco installed. Go to the chocolatey official website if you do not install it yet. When you have choco, you can install NVS by running this command
choco install nvs

Install NVS on Mac and Linux

You only need to run this command to install NVS on your mac / Linux.

export NVS_HOME="$HOME/.nvs"
git clone <https://github.com/jasongin/nvs> "$NVS_HOME"
. "$NVS_HOME/nvs.sh" install

The above command will add variable NVS_HOME which has value path $HOME/.nvs. For example, If my home directory is /Users/baggus/, then NVS_HOME is /Users/baggus/.nvs This location will be used by nvs to put nvs code and your Node JS installation.

The second line of the command is to clone the nvs repository and put it inside NVS_HOME directory. The last command, it will run nvs.sh, which will add nvs shell function to the environment. The install command will add lines on your ~/.bashrc, ~/.profile, or ~/.zshrc file to source nvs.sh (depending on what shell you use). With this command, the nvs command will be available in your shells.

After you have successfully installed nvs, you can verify the installation by run command on your shell/terminal

nvs --version

Managing multiple versions of Node JS via NVS

Now you have NVS installed on your computer. Let's start installing Node JS. In this example, I will show you how to install two versions of Node JS, and how to switch versions between them.

Actually, you have two options, you can use an interactive menu of nvs, or you can type your command as you want. For accessing the interactive menu, you run the command nvs without any parameter.

nvs

When the command executed, you will see the interactive menu like this.

Then you can choose a specific version of Node JS that you want to use. After you choose a version, nvs will download and install Node JS for you. Next time you run nvs command, you will see like this.

I choose to install Node vesion 14.17.2. It will show up on interactive menu. I think this is really easy so that you can do it on your own.

Next, I will show you how to do it via nvs command. In this example, I want to install the LTS version and the latest version. When I write this post, the LTS version is 14.17.2 and the latest version is 16.4.1

Follow these steps

  1. Install v14.17.2 (LTS)
nvs add 14.17.2

or you can also simply run

nvs add lts
  1. Check the list of Node version
nvm ls

You will see that version 14 there. It means that your installation of version 14 is successful. Your Node JS 14 is installed on ~/.nvs/node/14.17.2/

  1. Start to use version 14
nvs use 14

or

nvs use lts

The command nvs use is to switch versions for your current shell only. When you close your shell, the version used is still the default version. If you want to use it permanently (as your default), run nvs link lts

  1. Check the Node JS version you use
node -v

// will show output
// v14.17.2

Next, I will install version 16 (latest), and switch to that version.

  1. Install version 16
nvs add 16.4.1

or

nvs add latest
  1. Switch to that version
nvs use 16

or

nvs use latest
  1. Check Node version
node -v

// will show output
// v16.4.1

Now you have already familiar with the nvs command. It is really easy to use. I highly recommend you to use nvs or other node version managers before starting to develop a Node JS application.

To see the list of all nvs commands, check command reference on nvs repository.

References:

43