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
Linux/macOS (nvm-sh)
bash
nvm ls-remoteInstalling Node.js
To install a specific version of Node.js:
Windows (nvm-windows)
bash
nvm install <version>For example:
bash
nvm install 18.16.0Linux/macOS (nvm-sh)
bash
nvm install <version>For example:
bash
nvm install 18.16.0You can also install the latest LTS version:
bash
nvm install --ltsListing Installed Node.js Versions
To see all Node.js versions installed on your system:
Windows (nvm-windows)
bash
nvm list
Linux/macOS (nvm-sh)
bash
nvm lsSwitching 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.0Linux/macOS (nvm-sh)
bash
nvm use <version>For example:
bash
nvm use 18.16.0Checking Current Node.js Version
To see which Node.js version is currently active:
Windows (nvm-windows)
bash
nvm currentLinux/macOS (nvm-sh)
bash
nvm currentOr simply:
bash
node -vAdvanced 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.0Linux/macOS (nvm-sh)
bash
nvm alias default <version>For example:
bash
nvm alias default 18.16.0Using 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.
- Create a
.nvmrcfile with the version number:
bash
echo "18.16.0" > .nvmrc
- 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 useThis 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.0Linux/macOS (nvm-sh)
bash
nvm uninstall <version>For example:
bash
nvm uninstall 18.16.0Running 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.jsLinux/macOS (nvm-sh)
bash
nvm exec 18.16.0 node script.jsOr:
bash
nvm run 18.16.0 script.jsWorking 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.4Troubleshooting
Path Issues on Windows
If you're experiencing issues with NVM on Windows, it might be due to PATH conflicts. Try:
- Ensuring that NVM is properly added to your PATH
- Restarting your terminal or computer
- Running the terminal as administrator
Version Not Found
If you get an error that a version is not found:
- Check your internet connection
- Try using a mirror for faster downloads (see mirror configuration)
- Verify that the version exists by checking the available versions list
Permission Issues on Linux/macOS
If you encounter permission issues:
- Never use
sudowith NVM commands - Check the ownership of your
~/.nvmdirectory:
bash
ls -la ~/.nvm- Fix permissions if needed:
bash
chown -R $(whoami) ~/.nvm