NVM, or Node Version Manager, is a command-line tool that simplifies the process of managing multiple active Node.js versions. It lets you install, switch between, and manage different Node.js installations without conflicts.
Managing Node.js versions can be challenging, especially when different projects require different Node.js versions. Directly installing Node.js globally can lead to conflicts and broken projects. NVM solves this by providing isolated Node.js environments for each project.
Before installing NVM, it’s recommended that any existing Node.js or npm installations be removed to avoid potential conflicts.
The NVM install process on Unix-based systems is straightforward:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
(Replace v0.40.1
with the latest version number if needed. Check the NVM GitHub page for the latest version.)
export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
NVM also supports Windows, but it’s typically through WSL (Windows Subsystem for Linux), depending on the version of WSL. If that doesn’t work, you can try a separate project called NVM for Windows (nvm-windows):
Now that NVM is installed, let’s explore how to use it to manage your Node.js versions:
Before installing any Node.js versions, it’s helpful to see what’s available. You can do this by using the following command:
nvm ls-remote # On Unix systems nvm list available # On Windows ( nvm-windows )
The command will return a list of all the Node.js versions that NVM can install, including stable releases, LTS versions, and even older, less common versions.
If you are using nvm-windows make sure you run all nvm-windows commands in an Admin shell.
NVM makes installing Node.js versions a breeze. You have several options, depending on your needs.
To install the absolute latest version of Node.js, use the following command:
nvm install node # Unix nvm install latest # Windows ( nvm-windows )
At the time of writing, the latest version of Node.js is 23.7.0
.
For production environments, stability is key. Long Term Support (LTS) versions of Node.js are recommended for their extended support and reliability. To install the latest LTS version, use :
nvm install lts/* # Unix nvm install --lts # Unix nvm install lts # Windows ( nvm-windows )
At the time of writing, the latest LTS version of Node.js is 22.14.0
.
If your project requires a very specific Node.js version, NVM allows you to install any available version by specifying the version number. For example, to install version 18.12.0
, you would use the following command :
nvm install 18.12.0 # Works on both nvm and nvm-windows
To see which versions you have installed, run the following command:
nvm list # Works on both nvm and nvm-windows
The command will return the installed versions, such as:
v18.12.0 v22.14.0 -> v23.7.0 default -> lts/* (-> v22.14.0) iojs -> N/A (default) unstable -> N/A (default) node -> stable (-> v23.7.0) (default) stable -> 23.7 (-> v23.7.0) (default) lts/* -> lts/jod (-> v22.14.0) ...
The output above shows that the following versions of node were installed: 18.12.0, 22.14.0, 23.7.0
. It also shows that the current version of Node.js being used in this shell is v23.7.0
, and the default version is v23.7.0
. The nvm and nvm-windows output should be somewhat similar.
To switch to a specific installed version, use the nvm use <version>
command, replacing <version>
with the desired version number. For instance, to switch to version 18.12.0
, you would run the following command:
nvm use 18.12.0 # Works on both nvm and nvm-windows
To switch to the LTS version, you would run the following command instead:
nvm use lts/* # Unix nvm use lts # Windows ( nvm-windows )
Beyond the basic installation and usage, NVM offers advanced features that streamline your Node.js version management even further. These features allow you to fine-tune your NVM setup and customize it to fit your development workflow perfectly.
Let’s explore some of these more advanced capabilities:
Beyond simply switching between versions, you can configure NVM to automatically use a specific Node.js version as the default for all new terminal sessions. This is particularly useful for streamlining your workflow and ensuring that your projects always use the correct Node.js version.
To achieve this, use the nvm alias default <version>
command. Replace <version>
with the version number or alias (like lts/*
for the latest LTS) you want to use. For example:
nvm alias default 18.12.0
Or, for the latest LTS version:
nvm alias default lts/*
After setting the default, new terminal windows or tabs will automatically use the specified Node.js version. Existing terminals might need to be restarted or sourced again for the change to take effect.
Please note that the nvm alias
command isn’t available in nvm-windows.
If you need to remove a Node.js version that you no longer use, NVM makes this easy.
Use the nvm uninstall <version>
command, replacing <version>
with the version number you wish to remove. For example, to uninstall the Node.js version 23.7.0
using both NVM and nvm-windows you would run:
nvm uninstall 23.7.0 # Works on both nvm and nvm-windows
Managing Node.js versions on a per-project basis is crucial for ensuring compatibility and avoiding conflicts. NVM simplifies this with the .nvmrc
file. By placing a .nvmrc
file in the root directory of your project, you can specify the required Node.js version for that project.
To use this feature, simply create a file named .nvmrc
in your project’s root directory. Inside this file, put the Node.js version you need. For example, if your project requires Node.js 18.12.0
, your .nvmrc
file would contain:
18.12.0
Or, if you prefer to use the latest LTS version:
lts/*
When you navigate into a directory containing a .nvmrc
file, you can then run nvm use
. NVM will read the .nvmrc
file and automatically switch to the specified Node.js version. This makes collaboration easier, as anyone cloning the project can simply run nvm use
to use the correct Node.js version.
Please note that this feature isn’t available in nvm-windows.
In situations where the default Node.js download server is slow, unavailable, or you have specific mirroring requirements, the NVM_NODE_MIRROR
environment variable comes in handy. This variable allows you to specify an alternative URL from which NVM will download Node.js releases.
To use this, set the NVM_NODEJS_ORG_MIRROR
environment variable before installing Node.js versions. For example, in your shell configuration file (e.g., .bashrc
, .zshrc
), you might add:
export NVM_NODE_MIRROR=https://your-mirror.example.com/node/
Replace https://your-mirror.example.com/node/
with the actual URL of your preferred mirror. After setting this variable, NVM will use the specified mirror when downloading Node.js versions.
If you are using nvm-windows run the following command instead:
nvm node_mirror https://your-mirror.example.com/node/
Sometimes, you might need to run a single command in the context of a specific Node.js version without changing the default version for your current shell. The nvm exec
command is designed for this purpose.
The syntax is as follows:
nvm exec <version> -- <command>
Replace <version>
with the Node.js version you want to use, and <command>
with the command you want to execute. For example, to install project dependencies using npm with Node.js version 23.7.0
, you could use:
nvm exec v23.7.0 -- npm install
This will run npm install
using Node.js 23.7.0
, without affecting the Node.js version used by your current shell for other commands. This is very useful for running specific scripts or tools that require a different Node.js version than your project’s primary one.
Please note that the nvm exec
command isn’t available in nvm-windows.
While NVM generally works smoothly, you might occasionally encounter some issues. This section covers common problems and provides solutions to help you get NVM working correctly.
Before installing NVM, it’s crucial to remove any existing Node.js and npm installations. These pre-existing installations can conflict with NVM, leading to unexpected behavior and errors.
On Unix systems, this might involve using your distribution’s package manager (e.g., apt
, yum
, pacman
, Homebrew
) to remove Node.js and npm.
On Windows, uninstall via the Control Panel.
If the nvm
command isn’t recognized after installation, it usually means that NVM’s directory isn’t in your system’s PATH. To fix that:
nvm
command again. If that does not work, run the following commands for the different shells on the command line: source ~/.bashrc
(bash), source ~/.zshrc
(zhs). If the issue persists on macOS, check this linkOccasionally, you might encounter permission issues during NVM installation or when installing Node.js versions:
snap
, you will have to uninstall it and install it using apt
If you try to install or use a version of Node.js that NVM can’t find, it means that version isn’t available in the NVM repository.
Use nvm ls-remote
to see the complete list of available versions. Double-check the version number for typos.
To ensure a smooth and efficient experience with NVM, it’s helpful to follow some best practices. These guidelines will help you avoid potential problems and make the most of NVM’s features:
It’s strongly recommended that NVM be installed on a per-user basis, rather than globally on a shared system. This helps to isolate Node.js environments for different users and avoid conflicts.
Avoid using NVM in shared environments or on build servers where multiple users or processes might be using the same NVM installation. This can lead to issues with symbolic links and unpredictable behavior.
Consider using containerization technologies like Docker to manage Node.js versions in shared or automated environments.
Keeping NVM updated is important. New versions often include bug fixes, performance improvements, and support for the latest Node.js releases.
You can typically update NVM itself using a similar process to the initial installation. Consult the NVM GitHub page for update instructions.
For production environments, it’s generally best to use LTS versions of Node.js. LTS versions are supported for an extended period and receive critical bug fixes and security updates, ensuring stability for your applications.
Use nvm install lts/*
to install the latest LTS.
NVM simplifies Node.js version management, allowing you to switch between different versions and avoid conflicts easily. By following this tutorial, you can effectively manage your Node.js environments and ensure your projects run smoothly.
Explore the NVM git repository for more advanced features and options.
Deploying a Node-based web app or website is the easy part. Making sure your Node instance continues to serve resources to your app is where things get tougher. If you’re interested in ensuring requests to the backend or third-party services are successful, try LogRocket.
LogRocket is like a DVR for web and mobile apps, recording literally everything that happens while a user interacts with your app. Instead of guessing why problems happen, you can aggregate and report on problematic network requests to quickly understand the root cause.
LogRocket instruments your app to record baseline performance timings such as page load time, time to first byte, slow network requests, and also logs Redux, NgRx, and Vuex actions/state. Start monitoring for free.
Hey there, want to help make our blog better?
Join LogRocket’s Content Advisory Board. You’ll help inform the type of content we create and get access to exclusive meetups, social accreditation, and swag.
Sign up nowsort()
method: From basics to custom sortingDiscover how to sort arrays in JavaScript using sort() and toSorted(), including customization and language-sensitive sorting techniques.
With Redis’ license change and the launch of Valkey 8.0 as an alternative, this guide covers key differences and migration steps.
Compare GraphQL vs. REST APIs in terms of flexibility, efficiency, versioning, over-fetching, under-fetching, caching, and more.
Learn how to hide the scrollbar in popular web browsers by making use of modern CSS techniques, and dive into some interactive examples.