How to manage multiple Node.js Versions with NVM

Node.js is a versatile and popular runtime environment for executing JavaScript code on the server-side. However, different projects often require different Node.js versions due to compatibility or dependency constraints. Managing multiple Node.js versions can be challenging, but fortunately, there’s a tool that simplifies this process: Node Version Manager (NVM).

In this comprehensive guide, we will explore how to use NVM to manage multiple Node.js versions efficiently. Whether you’re a developer working on various projects or need to switch between Node.js versions for different applications, NVM is a valuable tool to have in your toolkit.

What is NVM?

Node Version Manager, commonly known as NVM, is a command-line utility that allows you to easily install, manage, and switch between different Node.js versions on a single machine. NVM is platform-agnostic and works on macOS, Linux, and Windows. It enables you to set specific Node.js versions on a per-project basis, ensuring compatibility with your applications.

Installing NVM (macOS)

To install NVM on macOS, follow these steps:

Step 1: Open Your Terminal

Open your terminal by using the Spotlight search or navigating to Applications > Utilities > Terminal.

Step 2: Install NVM

To install NVM, you can use the installation script provided on the official NVM GitHub repository. Run the following command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

This command downloads and runs the NVM installation script.

After the installation, you might need to close and reopen your terminal to use NVM. To verify the installation, run:

nvm --version

This should display the version of NVM you installed.

Managing Node.js Versions

Now that you have NVM installed, let’s explore how to use it to manage Node.js versions.

Installing Node.js

To install a specific Node.js version, use the nvm install command followed by the version number. For example, to install Node.js version 14, you can run:

nvm install 14

NVM will download and install the specified version.

Switching Between Node.js Versions

You can switch between installed Node.js versions using the nvm use command. For instance, to switch to Node.js version 14, run:

nvm use 14

NVM will set the selected version as the active one.

Setting a Default Node.js Version

To set a default Node.js version that will be used when you open a new terminal window, you can use the nvm alias command. For example, to set Node.js version 14 as the default, run:

nvm alias default 14

Now, whenever you open a new terminal window, Node.js version 14 will be active by default.

Listing Installed Node.js Versions

To see a list of Node.js versions installed on your system, you can use the nvm ls command:

nvm ls

This command will display a list of installed Node.js versions, including the one currently in use. An example response might look like this:

         v12.22.1
->       v14.17.0
         system
default -> v14.17.0
node -> stable (-> v14.17.0) (default)
stable -> 14.17 (-> v14.17.0) (default)
iojs -> N/A (default)
lts/* -> lts/fermium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.1
lts/fermium -> v14.17.0

Using .nvmrc Files

To ensure that your projects use specific Node.js versions, you can create a .nvmrc file in the project’s root directory. This file specifies the desired Node.js version. When you navigate to the project directory, NVM will automatically use the version specified in the .nvmrc file.

For example, create a .nvmrc file with the content 14 to specify Node.js version 14 for your project.

Conclusion

Node Version Manager (NVM) is an invaluable tool for managing multiple Node.js versions effortlessly. It allows you to switch between versions seamlessly, set project-specific Node.js versions, and ensure compatibility with your applications.

Whether you’re developing multiple projects or need to work with different Node.js versions for various reasons, NVM simplifies the process, making Node.js version management a breeze. By following this comprehensive guide, you’re well-equipped to handle the challenges of managing multiple Node.js versions effectively. Happy coding!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *