How to Manage Multiple Node.js Versions with NVM

Different projects often need different Node.js versions — NVM (Node Version Manager) lets you install and switch between multiple versions seamlessly on the same server.

Why You Need Version Management

A Node.js version that works for one project might be incompatible with another's requirements — NVM lets you maintain multiple installed versions and switch between them per-project or per-session, rather than being locked to one system-wide version.

Installing NVM

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc

Installing a Specific Node.js Version

nvm install 20
nvm install 18.19.0

Listing Installed Versions

nvm list

Switching Between Versions

nvm use 20
nvm use 18.19.0

Setting a Default Version

nvm alias default 20

New shell sessions automatically use this version unless overridden.

Using a Project-Specific Version (.nvmrc)

echo "20" > .nvmrc
nvm use

Reading from .nvmrc lets each project specify its exact required version, and anyone (including yourself later) can simply run nvm use in that directory to switch to the correct version automatically.

Automatically Switching Versions When Entering a Directory

nano ~/.bashrc
cd() {
    builtin cd "$@"
    if [ -f .nvmrc ]; then
        nvm use
    fi
}

Automatically activates the correct Node.js version whenever you cd into a directory containing an .nvmrc file, avoiding manual version switching.

Using NVM in Production/Non-Interactive Contexts (systemd Services)

NVM is primarily designed for interactive shell use — for systemd services, reference the specific NVM-installed Node binary path directly rather than relying on NVM's shell-based switching mechanism:

ExecStart=/home/user/.nvm/versions/node/v20.11.0/bin/node /opt/myapp/server.js

Removing an Installed Version

nvm uninstall 16

Updating NVM Itself

cd ~/.nvm
git fetch --tags origin
git checkout `git describe --abbrev=0 --tags`

Common Errors

"nvm: command not found" after installation — verify the NVM initialization lines were correctly added to your shell profile (.bashrc/.zshrc) and that you've either restarted your shell or sourced the profile file.

Continue Reading

Browse more articles in Programming Languages & Runtimes.

  • nvm node version manager, multiple node versions, nvmrc file, nvm use default
  • 0 Korisnici koji smatraju članak korisnim
Je li Vam ovaj odgovor pomogao?

Vezani članci

How to Install PHP on Ubuntu & Debian (Complete Guide)

PHP powers a huge share of the web, including WordPress, Laravel, and Magento. This guide covers...

How to Install Node.js on Ubuntu & Debian (with NVM)

Node.js is a fast JavaScript runtime for building APIs, web applications, and backend services....

How to Install Python on Ubuntu & Debian

Python powers web frameworks like Django and Flask, automation scripts, and data applications....

How to Install Java (OpenJDK) on Ubuntu & Debian

Java powers enterprise applications, build tools like Maven and Gradle, and platforms like...

How to Install Composer for PHP Dependency Management

Composer is PHP's standard dependency manager, used by virtually every modern PHP framework...