How to Manage Multiple Node.js Versions with nvm

Category: Software Install and Setup

Node.js is a powerful runtime for building JavaScript applications, but different projects may require different Node.js versions. Managing these versions manually can be challenging, which is why nvm (Node Version Manager) is an essential tool for developers. This guide will show you how to install and use nvm to manage multiple Node.js versions efficiently.

1. What is nvm?

nvm (Node Version Manager) is a command-line utility that allows developers to install, switch, and manage multiple versions of Node.js seamlessly.

2. Installing nvm

To use nvm, you first need to install it.

For macOS and Linux:

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

Once installed, restart your terminal and verify the installation:

command -v nvm

For Windows:

Download and install the nvm-windows setup file.

3. Installing Node.js Versions

To install a specific Node.js version, use:

nvm install 16.14.2

To list all available Node.js versions:

nvm ls-remote

4. Switching Between Node.js Versions

To switch to a different Node.js version:

nvm use 14.17.0

To set a default version for new sessions:

nvm alias default 16.14.2

5. Listing Installed Versions

To check which Node.js versions are installed on your system:

nvm list

6. Removing an Old Node.js Version

To remove an old Node.js version that is no longer needed:

nvm uninstall 14.17.0

7. Using Different Node.js Versions for Projects

To ensure a project uses a specific Node.js version, create an .nvmrc file inside your project directory with the version number:

echo "16.14.2" > .nvmrc

Then, inside the project directory, run:

nvm use

8. Troubleshooting nvm Issues

If you run into issues using nvm, try the following solutions:

  • Ensure nvm is loaded by running:
source ~/.nvm/nvm.sh
  • For Windows users, restart your system and try again.
  • If a command is not recognized, reinstall nvm and verify the installation.

Conclusion

nvm makes it easy to manage multiple Node.js versions, allowing you to work on different projects without conflicts. By following this guide, you can install, switch, and remove Node.js versions effortlessly. For more details, check the official nvm documentation.