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)
nvm list available
Linux/macOS (nvm-sh)
nvm ls-remote
Installing Node.js
To install a specific version of Node.js:
Windows (nvm-windows)
nvm install <version>
For example:
nvm install 18.16.0
Linux/macOS (nvm-sh)
nvm install <version>
For example:
nvm install 18.16.0
You can also install the latest LTS version:
nvm install --lts
Listing Installed Node.js Versions
To see all Node.js versions installed on your system:
Windows (nvm-windows)
nvm list
Linux/macOS (nvm-sh)
nvm ls
Switching Node.js Versions
To switch to a specific Node.js version:
Windows (nvm-windows)
nvm use <version>
For example:
nvm use 18.16.0
Linux/macOS (nvm-sh)
nvm use <version>
For example:
nvm use 18.16.0
Checking Current Node.js Version
To see which Node.js version is currently active:
Windows (nvm-windows)
nvm current
Linux/macOS (nvm-sh)
nvm current
Or simply:
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)
nvm alias default <version>
For example:
nvm alias default 18.16.0
Linux/macOS (nvm-sh)
nvm alias default <version>
For example:
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.
- Create a
.nvmrc
file with the version number:
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:
nvm use $(type .nvmrc)
Linux/macOS (nvm-sh)
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)
nvm uninstall <version>
For example:
nvm uninstall 18.16.0
Linux/macOS (nvm-sh)
nvm uninstall <version>
For example:
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:
nvm use 18.16.0 && node script.js
Linux/macOS (nvm-sh)
nvm exec 18.16.0 node script.js
Or:
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.
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:
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:
- 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
sudo
with NVM commands - Check the ownership of your
~/.nvm
directory:
ls -la ~/.nvm
- Fix permissions if needed:
chown -R $(whoami) ~/.nvm