Skip to content

Install NVM

Different projects often require different versions of Node.js. NVM (Node Version Manager) makes it easy to install, manage, and switch between multiple Node versions on your machine, ensuring you can work on any project without version conflicts.


NVM (Node Version Manager) is a powerful tool that allows you to easily install and manage multiple versions of Node.js on your system.

We use NVM because it:

  • Allows switching between Node.js versions with simple commands
  • Enables working on projects with different Node requirements
  • Prevents version conflicts across projects
  • Makes it easy to test code on different Node versions
  • Doesn’t require administrator/root privileges for Node installations
  • Keeps your system clean with isolated Node installations

Launch your terminal application.

Execute the following curl command to download and install NVM:

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

This will download and run the NVM installation script.

To verify the installation, run:

Terminal window
command -v nvm

If you see nvm as output, the installation was successful.

04. Fix Developer Tools Error (macOS only)

Section titled “04. Fix Developer Tools Error (macOS only)”

You might see an error: “nvm xcrun error invalid active developer path.”

To solve this, install Xcode Command Line Tools:

Terminal window
xcode-select --install

After completing the previous steps, verify NVM is working:

Terminal window
nvm -v

You should see the version number (e.g., 0.39.3).


Fix: NVM Not Loading Automatically in Terminal

Section titled “Fix: NVM Not Loading Automatically in Terminal”

Sometimes after installing NVM, you may notice that it’s not available in new terminal sessions, even though the installation completed successfully.

This usually happens because the installation script failed to correctly update your shell configuration file (.zshrc or .bashrc).

During installation, NVM attempts to append the necessary environment variables to your shell config file. However, depending on:

  • Your shell (zsh, bash, etc.)
  • Custom configurations or permission issues
  • Using tools like Oh My Zsh or custom terminal environments

…the required lines might not be added automatically, or may be placed incorrectly.

You need to manually add the required lines to your shell configuration file.

Edit your .zshrc:

Terminal window
nano ~/.zshrc

Add the following at the end of the file:

Terminal window
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Save the file (Ctrl+O, Enter, then Ctrl+X), then reload it:

Terminal window
source ~/.zshrc

Edit your .bashrc:

Terminal window
nano ~/.bashrc

Add the same lines at the end:

Terminal window
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Save and reload:

Terminal window
source ~/.bashrc

After applying the changes, open a new terminal and run:

Terminal window
nvm -v

You should now see the installed version, confirming that NVM loads correctly every time.


First, see what versions you currently have installed:

Terminal window
nvm ls

Initially, you might only see:

-> system
iojs -> N/A (default)
node -> stable (-> N/A) (default)
unstable -> N/A (default)

Install the Node.js versions you need for your projects. For example:

Terminal window
nvm install 14
nvm install 16
nvm install 18
nvm install 20

To use a specific version:

Terminal window
nvm use 16

To set a default version:

Terminal window
nvm alias default 18

Once you have multiple versions installed, check them with:

Terminal window
nvm ls

versions

The arrow (->) indicates which version is currently active.


CommandDescription
nvm install <version>Install a specific Node.js version
nvm use <version>Switch to a specific version
nvm lsList all installed versions
nvm ls-remoteList all available versions to install
nvm alias default <version>Set a default Node.js version
nvm currentDisplay the currently active version
nvm which <version>Show the path to a specific version
nvm uninstall <version>Remove a specific version

  • Install the LTS (Long Term Support) versions for production projects
  • Use the latest version for experimenting with new features
  • Set a sensible default version using nvm alias default <version>
  • Switch to the project’s required Node version before running npm install
  • Consider using a .nvmrc file in your project root to specify the required Node version

Knowledge Check

Test your understanding of this section

Loading questions...