DEV Community

Cover image for Manage Node.js Versions With n
Jesse Wei
Jesse Wei

Posted on

Manage Node.js Versions With n

One of the first challenges you may come across when starting to learn Javascript is how to deal with multiple versions of Node.js.

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.

Why Multiple Versions of Node.js?

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.

How to Manage Node.js Versions: Meet n

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.

Installation

If you have Node.js installed, you can just run:

npm install -g n
Enter fullscreen mode Exit fullscreen mode

to install 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 n with npm:

curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
bash n lts
npm install -g n
Enter fullscreen mode Exit fullscreen mode

Other installation options include installing with brew or port for Mac users and installing from github. Check out the README of the n repo for more information.

After Installation

After 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
Enter fullscreen mode Exit fullscreen mode

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 n or npm. Find out more about this from the installation guide.

Try It Out

To see versions of Node.js available for downloading:

n ls-remote
Enter fullscreen mode Exit fullscreen mode

This lists the latest 20 versions, but if you want to list all versions, add the --all option:

n ls-remote --all
Enter fullscreen mode Exit fullscreen mode

To see downloaded versions of Node.js:

n
Enter fullscreen mode Exit fullscreen mode

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:

n <version>
Enter fullscreen mode Exit fullscreen mode

To download the latest version of Node.js:

n lts
Enter fullscreen mode Exit fullscreen mode

To switch versions of Node.js, output downloaded versions first with:

n
Enter fullscreen mode Exit fullscreen mode

and then, use arrow keys to select a version and press Enter (Return) to confirm:

  ο node/14.18.1
    node/19.2.0
Enter fullscreen mode Exit fullscreen mode

For more information on the usage of n, check out its docs.

Conclusion

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 n manager.

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.

Top comments (0)