Introduction

nvm (Node Version Manager) is a command-line tool that allows developers to easily switch between different versions of Node.js, the JavaScript runtime that allows developers to run JavaScript on the server side. It allows you to install multiple versions of Node.js on a single machine, and easily switch between them. This is useful for testing and development, as it allows you to ensure that your code will work on different versions of Node.js.

From the official documentation, here are some examples on how to easily use nvm:

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

Installation

It is possible to install nvm using Homebrew, but it is mentioned from the official documentation that the Homebrew installation is not supported. Thus, we will follow the official steps, which is also a simple process.

  1. Open terminal
  2. Download the nvm installation script by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash

This will download the installation script and run it. If an older version of nvm is installed, this script will update it.

3. Once the installation script is finished, you can configure your terminal to use nvm by adding the following line to your .bash_profile or .bashrc file:

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

4. To check the installation, run the command nvm --version to confirm that nvm is installed and its version.

Usage

  • To install the latest LTS release of Node.js, use the following command:
nvm install --lts
  • To install the latest Node.js version:
nvm install latest
  • To install a specific version of Node.js:
nvm install 14.7.0 # Replace 14.7.0 with the target version
  • To list all versions currently installed:
nvm ls
  • To use a specific version of Node.js, use the following command:
nvm use 14.7.0 # Replace 14.7.0 with the target version
  • To list available versions that can be installed:
nvm ls-remote
  • To check current nvm version:
nvm --version

It's important to note that you should check the nvm installation script link as it may change over time, and you should use the latest version available on the nvm Github repository.