Today, let's find out why you would need multiple versions of Node.js in the first place and how to manage them efficiently with n, a Node.js version manager that is very developer-friendly.
The main reason why you may need multiple versions of Node.js is compatibility.
Different versions of Node.js can be incompatible with certain packages or libraries and different projects are usually developed using different versions of Node.js.
For example, a package that was built for Node.js version 8 might not work with version 12. Also, you may have to work on a project that is developed with Node.js version 14 but you only have version 18 installed in your local computer.
By managing multiple versions of Node.js, you can ensure that your application is compatible with the packages and libraries it depends on and you can choose the right version to use for a particular project.
Now that we see why we may need multiple versions of Node.js, let's take a look at how to manage them.
Although we can just head to the official website of Node.js, download and install it, it is only one version and we cannot keep multiple versions of Node.js on our computer this way.
That's where Node.js managers come into play. A Node.js manager is itself a package, but it can help us install multiple versions of Node.js and switch between the versions easily.
The Node.js manager I want to introduce today is n, which is super easy to install and use.
If you have Node.js installed, you can just run:
npm install -g n
If you haven't had Node.js installed yet, you can run these commands to first install the latest version of Node.js (to make
npm available) and then install
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n bash n lts npm install -g n
Other installation options include installing with
port for Mac users and installing from github. Check out the README of the
n repo for more information.
n is installed, let's run:
sudo mkdir -p /usr/local/n sudo chown -R $(whoami) /usr/local/n sudo mkdir -p /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
These commands look daunting but basically what we're doing here is just take ownership of the
n directory as well as other relevant system directories. This way, we don't have to use
sudo to install packages with
npm. Find out more about this from the installation guide.
To see versions of Node.js available for downloading:
This lists the latest 20 versions, but if you want to list all versions, add the
n ls-remote --all
To see downloaded versions of Node.js:
It's as simple as that. This command output all versions that you've downloaded with the active version highlighted.
To download a specific version of Node.js:
To download the latest version of Node.js:
To switch versions of Node.js, output downloaded versions first with:
and then, use arrow keys to select a version and press Enter (Return) to confirm:
ο node/14.18.1 node/19.2.0
For more information on the usage of
n, check out its docs.
In this post, we took a quick look at why we might want to manage multiple versions of Node.js and how we can do it by using the
There are a couple of other alternatives to
n, but I find
n the easiest to get used to and its docs are great. Try it out now and I think you'll like it.