If you work on multiple Node.js projects, you’ve probably run into this one time or another. You have the latest and greatest version of Node.js installed, and the project you’re about to work on requires an older version. In those situations, the Node Version Manager (nvm) is a great tool to use, allowing you to install multiple versions of Node.js and switch between them as you see fit.
In this tutorial, you will install
nvm and learn to install, remove, and switch between different versions of Node.js.
To complete this tutorial, you will need the following:
- The latest version of Node installed on your machine. To install Node on macOS, follow the steps outlined in this How to Install Node.js and Create a Local Development Environment on macOS tutorial.
Step 1 — Getting Started
To get started, you will need to install the Node Version Manager, or
nvm, on your system. You can install it manually by running the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
If you prefer
wget, you can run this command:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Then you will see an output similar to below:
Running either of the above commands downloads a script and runs 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 (
~/.bashrc). If you don’t see in our profile file you can add the following lines to end of the file as follows.
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
nvm installed, then restart your terminal and see if you can check the nvm version as follows.
If you are facing any issues you can check the troubleshooting guide in order to fix any common errors. The next step is to install and work with multiple versions of Node.js.
Step 2 — Installing Multiple Node.js Versions
To download, compile, and install the latest release of node, do this:
nvm install node # "node" is an alias for the latest version
To install a specific version of node:
nvm install 6.14.4 # or 10.10.0, 8.9.1, etc
Installing LTS version of a given version:
If you want to install an LTS version of a given release, you can just mention the version number as follows to install particular LTS versions of that release.
nvm install 12
Then, You will see an output as follows:
The first version installed becomes the default. New shells will start with the default version of node (e.g.,
nvm alias default).
Changing the default version of node:
During the time of the release of this tutorial, the latest stable version of Node.js is version 15.12.0.
To set the latest stable version as your default, run:
nvm alias default stable
You may also have a specific version number you would like to set as your default. To alias default to a specific version, run:
nvm alias default 12
Step 3 — Listing Installed Node.js Versions
With a handful of different versions of Node.js installed, we can run
nvm with the
ls argument to list out everything we have installed:
The output produced by running this command might look something like this:
Your output will probably differ depending on how many versions of Node.js you have installed on your machine.
-> indicates the active version and
default -> indicates the default version of Node.js. The default version of Node is the version that will be available when you open a new shell.
system corresponds with the version of Node.js installed outside of
nvm on your system.
Step 4— Switching Between Node.js Versions
To switch to a different version of Node.js, use the
use followed by the version of Node.js you would like to use:
nvm use 8
This is the output you will see:
You can even switch back to your default version:
nvm use default
At this point, you have installed several versions of Node.js. You can use
nvm to uninstall any unwanted version of Node.js you may have.
Step 6 — Removing Node.js Versions
You may have several versions of Node.js installed due to working on a variety of projects on your machine.
Fortunately, you can remove Node.js versions just as easily as you installed them:
nvm uninstall 8
Unfortunately, when you specify a major or minor version,
nvm will only
uninstall the latest installed version that matches the version number.
So, if you have two different versions of Node.js version 6 installed, you have to run the
uninstall command for each version:
$ nvm uninstall 6
Uninstalled node v6.14.4$ nvm uninstall 6
Uninstalled node v6.10.3
It’s worth noting that you can’t remove a version of Node.js that is currently in use and active.
You may want to return to your system’s default settings and stop using nvm. The next step will explain how to do this.
Step 7 — Un installing Node Version Manager(NVM)
If you would like to completely remove
nvm from your machine, you can use the
Working on multiple projects that use different versions of Node.js doesn’t have to be a nightmare. Node Version Manager makes the process seamless. If you would like to avoid having to remember to switch versions, you can take things a step further by creating a
.nvmrc file in your project’s root:
$ echo "12" > .nvmrc
Hope I have covered everything you need to know in order to manage multiple version of node in your Mac OS. Also, you can look at the following articles on how you can install and use multiple versions of Java and Python in Mac OS.