Manage Node with Node Version Manager (NVM)

The good old fashion way of installing NodeJS and NPM is by downloading the binary installer from NodeJS’s home page. This isn’t a bad approach, but what if there was another way.

Let me introduce you to Node Version Manager (NVM).

NVM is a version manager for NodeJS, 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.

There are a few benefits using NVM over the traditional binary installer. A couple include:

  • The ability to install and switch between different node versions due to legacy code
  • Avoiding sudo when installing node packages globally

The beautiful thing about NVM is that all your node installations are stored within an .nvm folder in your home directory.

The binary installation is usually installed at a system level and you won’t be able to install global packages unless you have admin rights or use sudo.

To install or update nvm, you can run the install script using Wget.

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

Running the above command will download a script and execute it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

To install the latest version of Node, run the following command:

nvm install node

To install a specific version of Node, run the following command:

nvm install 12.16.1

To see all available versions of Node available, run the following command:

nvm ls-remote

You can install as many versions of Node as you want and won’t need to run sudo or root when installing packages globally.

To switch to another version of Node, simply run the following command:

nvm use v10.17.0

Lastly, you might have an occurrence where nvm keeps “forgetting” Node in new terminal session.

To correct this, simply run the following command below to default to a particular version of Node.

nvm alias default 12.16.1

I hope you found this post useful and if you’re not using Mac or Linux, you can alternatively get a version of NVM for Windows.