Manage Node.js Versions With n
Tags: webdev, node, beginners, tutorial
Published: 2022-12-28 00:00:00

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](https://github.com/tj/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](https://github.com/tj/n), which is super easy to install and use.

### Installation

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

```bash
npm install -g n
```

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`:

```bash
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 `brew` or `port` for Mac users and installing from github. Check out the [README](https://github.com/tj/n#readme) of the `n` repo for more information.

### After Installation

After `n` is installed, let's run:

```bash
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 `n` or `npm`. Find out more about this from the [installation guide](https://github.com/tj/n#installation).

## Try It Out

To see versions of Node.js available for downloading:

```bash
n ls-remote
```

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

```bash
n ls-remote --all
```

To see downloaded versions of Node.js:

```bash
n
```

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:

```bash
n <version>
```

To download the latest version of Node.js:

```bash
n lts
```

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

```bash
n
```

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

```bash
  ο node/14.18.1
    node/19.2.0
```

For more information on the usage of `n`, check out its [docs](https://github.com/tj/n#installing-nodejs-versions).

## 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.