NodeJS is a runtime environment that allows you to develop and run standalone software written in JavaScript. It comes with the node package manager (NPM) that provides access to nearly 500 thousand packages. In order to

  • easily install the most recent version of NodeJS and NPM

  • switch between different versions of NodeJS and NPM

  • install NodeJS and NPM in your home directory (instead of system wide)

you can use the node version manager (NVM). Since I’m using Zsh (along with Oh-My-Zsh) as my main login shell, I install and update NVM using the Zsh-plugin Zsh-NVM.

This article describes how to install and update NVM using Zsh-NVM and how to use NVM to manage your NodeJS/NPM installations. If you have not used Zsh and Oh-My-Zsh yet, you might want to read the article "Installation of Zsh and Oh My Zsh". Using NPM and NodeJS will be described in a separate article.

Install Zsh-NVM and NVM

The following procedure describes how to install Zsh-NVM as an Oh-my-Zsh Plugin. There are also other instructions available for the installation of Zsh-NVM.

  1. Clone the Zsh-NVM repository into the custom plugins folder of your Oh-My-Zsh installation (typically ~/.oh-my-zsh/custom/plugins/)

    git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm
  2. Load Zsh-NVM as a Zsh-plugin. Therefore simply add plugins+=(zsh-nvm) to your ~/.zshrc.

    Keep in mind that plugins need to be added before oh-my-zsh.sh is sourced.

  3. When opening a new terminal window, NVM will be automatically installed by Zsh-NVM.

Upgrade NVM

NVM-Zsh provides two additional commands to ease the update of NVM.

  • nvm upgrade installs the latest version of NVM

  • nvm revert switches back to the previous version of NVM

Uninstall NVM

To uninstall NVM

  1. Remove the folder stored in $NVM_DIR (usually ~/.nvm)

  2. Remove the line plugins+=(zsh-nvm) from your ~/.zshrc (otherwise NVM will be reinstalled when opening a new terminal window)

Use NVM to manage your NodeJS installations

List NodeJS versions

NVM allows you to install and switch between several versions of NodeJS (and NPM). A common task is to find out which versions are available on your system and on the internet.

  • nvm ls lists all locallay installed NodeJS versions

  • nvm ls-remote lists all available versions of NodeJS

The output of nvm ls shows the versions of NodeJS that are currently installed on your system. The currently active version is preceeded with an arrow (). At the end of the list aliases and Long-term Support (LTS) versions are shown.

Image showing the output of the command 'nvm ls' on a system that has two versions (7.2.1 and 8.4.0) of NodeJS installed.
Figure 1. Image showing the output of the command 'nvm ls' on a system that has two versions (7.2.1 and 8.4.0) of NodeJS installed.

The following table explains the color code of the output of nvm ls.

Style Meaning

green

Active NodeJS version

blue

Non-active NodeJS versions

bold red

Not installed NodeJS versions (and aliases)

bold yellow

Aliases of LTS NodeJS versions

(Un-)install NodeJS

You can install an uninstall NodeJS versions by either using pre-defined aliases or version numbers (NodeJS uses Semantic Versioning).

Command Purpose

nvm install stable

Install the latest stable version of NodeJS

nvm install --lts or nvm install lts/*

Install the latest LTS version of NodeJS

nvm install 8

Install the latest 8.x.x version of NodeJS

nvm install 6.1

Install the latest 6.1.x version of NodeJS

nvm install 7.2.1

Install a specific version of NodeJS (here 7.2.1)

You can use the uninstall command just like the install commands. For example nvm uninstall 5 will remove the latest 5.x.x version of NodeJS from your system.

Switch between NodeJS/NPM versions

After installing a new NodeJS version, that version is activated in your working shell.

You can run different NodeJS versions in different terminal windows in parallel.

For changing the NodeJS version in your current terminal, you can use nvm use …​ as shown in the table below.

Command Purpose

nvm use stable

Use the latest stable version of NodeJS

nvm use --lts or nvm use lts/*

Use the latest LTS version of NodeJS

nvm use 8

Use the latest 8.x.x version of NodeJS

nvm use 6.1

Use the latest 6.1.x version of NodeJS

nvm use 7.2.1

Use a specific version of NodeJS (here 7.2.1)

The command nvm use …​ will not change the default NodeJS version for your account. Thus, when a new shell is opened, the version that is linked to the alias default is used (also see NVM keeps forgetting Node in new terminal session).

In order to change/set the default NodeJS version you have to change the alias default (also see Set default Node version with NVM).

Command Purpose

nvm alias default stable

Use the latest stable version as the default version of NodeJS

nvm alias default --lts or nvm alias default lts/*

Use the latest LTS version as the default version of NodeJS

nvm alias default 8

Use the latest 8.x.x version as the default version of NodeJS

nvm alias default 6.1

Use the latest 6.1.x version as the default version of NodeJS

nvm alias default 7.2.1

Use a specific version as the default version of NodeJS (here 7.2.1)

Keep in mind that all globally installed NodeJS tools need to be reinstalled for each new version. Personally I like this fact, since it reminds you of the tools you depend on and how you installed them.

The command nvm reinstall-packages …​ allows you to easily install all global NodeJS packages to your current version, that you installed in another NodeJS version.

Example:

  • If you used version 7 of NodeJS and

  • you switched to version 8

  • than simply run nvm reinstall-packages 7 to install all packages to NodeJS 8, that were installed in your installation of NodeJS 7

Regular Tasks

In order to keep your system up to date, you should regularly run the following commands

  • nvm upgrade to keep your installation of NVM up to date.

  • nvm ls-remote to check, if newer versions of NodeJS are available.

  • nvm install latest to install the latest version of NodeJS.