Node Version Manager (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, Windows WSL.”

NVM Repository Readme

Introduction

Language version managers are a necessity for a number of languages (e.g. Python, Ruby) and JavaScript is no exception. nvm provides a simple way to install node.js, including multiple versions simultaneously.

Even if you aren’t planning on working with multiple node.js installs simultaneously using nvm can help isolate system changes making it easier to disentangle oneself from any given node.js install in the future (e.g., if like me you invariably manage to mangle the environment beyond repair.

This page isn’t meant to teach you everything about nvm or to replace the README.md for nvm. It is meant to provide a streamlined and opinionated guide on using nvm that will hopefully work on most systems for most people.

Installation

There are multiple ways to install nvm, but lets keep it to one here:

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

nvm is installed in the ~/.nvm directory. nvm also adds to one’s *sh configuration file (e.g. .bash_profile) :

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

Basically, it ensures that you can run nvm from anywhere without needing to profile the full path to the executable.

Using nvm

  • To install latest release of node: nvm install node (don’t recommend this, use the lts instead)
  • To install a specific version of node: nvm install x.x.x, for example, nvm install 14.17.1
  • You can install the latest LTS: nvm install --lts (use this in most cases)
  • To get a list of all the node versions available for install: nvm ls-remote
  • To get a list of all node versions on your local system: nvm ls
  • To use node: nvm use node
  • To run a specific version of node: nvm use x.x.x, for example, nvm use 14.17.1
  • To get the path to the executable for a specific node version: nvm which x.x, for example, nvm which 14.17.1
  • To migrate existing packages from older node to new: nvm install 'lts/*' --reinstall-packages-from=current
  • To upgrade npm to latest: nvm install-latest-npm
  • To use system node (if it exists): nvm use system
  • To remove nvm: rm -rf "$NVM_DIR" and remove nvm related lines from *sh config file

Per Project Configurations

Add a .nvmrc file to the root of your project directory and specify a version, e.g. 14.17.1 in the file. If you don’t specify a specific node version when using nvm commands (such as nvm use, it will default to that specified in the .nvmrc file. You can use this command in the project root to both create and populate the .nvmrc file:

echo "x.x.x" > .nvmrc

For example:

echo "14.17.1" > .nvmrc

Default Global Packages

You can create a list of the packages you want installed with every node version. Add them one per line to $NVM_DIR/default-packages, in most cases this will be: ~/.nvm/default-packages. Enter one package per line.