Skip to content

Using NVM

This guide covers the basic usage of NVM (Node Version Manager) for both Windows and Unix-like systems (Linux, macOS, WSL).

Basic Commands

Listing Available Node.js Versions

To see all available Node.js versions that you can install:

Windows (nvm-windows)

bash
nvm list available

NVM List Available

Linux/macOS (nvm-sh)

bash
nvm ls-remote

Installing Node.js

To install a specific version of Node.js:

Windows (nvm-windows)

bash
nvm install <version>

For example:

bash
nvm install 18.16.0

Linux/macOS (nvm-sh)

bash
nvm install <version>

For example:

bash
nvm install 18.16.0

You can also install the latest LTS version:

bash
nvm install --lts

Listing Installed Node.js Versions

To see all Node.js versions installed on your system:

Windows (nvm-windows)

bash
nvm list

NVM List

Linux/macOS (nvm-sh)

bash
nvm ls

Switching Node.js Versions

To switch to a specific Node.js version:

Windows (nvm-windows)

bash
nvm use <version>

For example:

bash
nvm use 18.16.0

Linux/macOS (nvm-sh)

bash
nvm use <version>

For example:

bash
nvm use 18.16.0

Checking Current Node.js Version

To see which Node.js version is currently active:

Windows (nvm-windows)

bash
nvm current

Linux/macOS (nvm-sh)

bash
nvm current

Or simply:

bash
node -v

Advanced Usage

Setting a Default Node.js Version

To set a default Node.js version that will be used when opening a new terminal:

Windows (nvm-windows)

bash
nvm alias default <version>

For example:

bash
nvm alias default 18.16.0

Linux/macOS (nvm-sh)

bash
nvm alias default <version>

For example:

bash
nvm alias default 18.16.0

Using Project-Specific Node.js Versions with .nvmrc

You can create a .nvmrc file in your project's root directory to specify which Node.js version should be used for that project.

  1. Create a .nvmrc file with the version number:
bash
echo "18.16.0" > .nvmrc

NVMRC File

  1. Use the specified version:

Windows (nvm-windows)

With nvm-windows, you need to manually read the .nvmrc file and use the specified version:

bash
nvm use $(type .nvmrc)

Linux/macOS (nvm-sh)

bash
nvm use

This will automatically read the version from the .nvmrc file.

Uninstalling Node.js Versions

To uninstall a specific Node.js version:

Windows (nvm-windows)

bash
nvm uninstall <version>

For example:

bash
nvm uninstall 18.16.0

Linux/macOS (nvm-sh)

bash
nvm uninstall <version>

For example:

bash
nvm uninstall 18.16.0

Running a Command with a Specific Node.js Version

Windows (nvm-windows)

nvm-windows doesn't directly support running a command with a specific Node.js version. You need to switch versions first:

bash
nvm use 18.16.0 && node script.js

Linux/macOS (nvm-sh)

bash
nvm exec 18.16.0 node script.js

Or:

bash
nvm run 18.16.0 script.js

Working with npm

When you switch Node.js versions using NVM, npm is also switched to the version bundled with that Node.js version.

Installing Global npm Packages

When you install global npm packages, they are installed in the context of the current Node.js version. To make a package available across all Node.js versions, you need to reinstall it for each version.

bash
npm install -g <package-name>

Using npm with Different Node.js Versions

If you want to use a specific npm version with a Node.js version:

bash
nvm use 18.16.0
npm install -g npm@9.6.4

Troubleshooting

Path Issues on Windows

If you're experiencing issues with NVM on Windows, it might be due to PATH conflicts. Try:

  1. Ensuring that NVM is properly added to your PATH
  2. Restarting your terminal or computer
  3. Running the terminal as administrator

Version Not Found

If you get an error that a version is not found:

  1. Check your internet connection
  2. Try using a mirror for faster downloads (see mirror configuration)
  3. Verify that the version exists by checking the available versions list

Permission Issues on Linux/macOS

If you encounter permission issues:

  1. Never use sudo with NVM commands
  2. Check the ownership of your ~/.nvm directory:
bash
ls -la ~/.nvm
  1. Fix permissions if needed:
bash
chown -R $(whoami) ~/.nvm

Additional Resources

Built with VitePress