Definition

NVM is a version manager for Node.js that allows you to install and switch between different versions of Node.js. It’s particularly useful when working on different projects that require different Node.js versions. It’s particularly useful when working on different projects that require different Node.js versions.

Installation

Here’s how to get started with NVM in WSL2 Ubuntu:

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

Commands

# List available Node.js versions
nvm ls-remote
 
# Install a specific version
nvm install 20.11.0
 
# Use a specific version
nvm use 20.11.0
 
# Set default Node.js version
nvm alias default 20.11.0
 
# List installed versions
nvm ls
# or
nvm list
 
# Check current version
nvm current

You can also use .nvmrc file in your project root to automatically switch Node versions. Simply create a file named .nvmrc with your desired Node version:

echo "20.11.0" > .nvmrc

Then use nvm use in the project directory to automatically switch to that version.

# To make a specific version the default:
nvm alias default v23.5.0
 
# Ensure node version is the same with nvm
node -v
# This should return `v23.5.0`.
 
npm -v
# This should return the version of npm that is compatible with node.js