Setting Up Node Version Manager (NVM)

April 10, 2023 (2y ago)

Setting Up Node Version Manager (NVM)

Node Version Manager (NVM) is an essential tool for JavaScript developers, allowing you to install and switch between multiple versions of Node.js. This is particularly useful when working on different projects that require different Node.js versions.

Installation

macOS and Linux

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

Windows

For Windows, you can use nvm-windows:

  1. Download the installer from the releases page
  2. Run the installer and follow the instructions

Basic Usage

List Available Node.js Versions

nvm ls-remote

Install a Specific Node.js Version

nvm install 16.15.0

Use a Specific Node.js Version

nvm use 16.15.0

Set Default Node.js Version

nvm alias default 16.15.0

Project-Specific Configuration

Create a .nvmrc file in your project directory with the Node.js version:

echo "16.15.0" > .nvmrc

Then simply run nvm use in that directory to automatically switch to the specified version.

Troubleshooting

If you encounter issues with the nvm command not being found, ensure that the following is in your shell profile (.bashrc, .zshrc, etc.):

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

By using NVM, you can easily manage multiple Node.js environments and ensure compatibility across different projects.