How to Set the Default Node.js Version using NVM [5 Ways]

Node Version Manager (NVM) is a popular tool to manage multiple Node.js versions on a single system.

Here are five ways to set your default Node.js version using NVM:

  1. Using the nvm alias default command
  2. Using nvm use with .nvmrc
  3. Automatically switch Node version
  4. Using nvm install
  5. Using the NVM GUI

Method 1: Using the nvm alias default command

The easiest way to set the default Node.js version using NVM in your system is to use the “nvm alias default” command.

nvm alias default 20.6.1

At the time of this article, the latest version of Node.js is 20.6.1. So, we are setting that as a default version.

Set the Default Node.js Version using NVM

To set the default version to 18.16.0, you should run this command:

nvm alias default 18.16.0

Method 2: Using nvm use with .nvmrc

You can create a .nvmrc file in your project’s root (or any directory) with the desired Node.js version.

For instance, if you want to set version 20.6.1 as the default, the .nvmrc file would simply contain:

20.6.1

Whenever you navigate to that directory in the terminal, you can automatically switch to that version using:

nvm use

While this doesn’t set a global default, this can be helpful for project-specific defaults.

Method 3: Automatically switch Node version

You can automate switching Node versions by combining the .nvmrc method with a shell script.

Add the following to your shell profile (like .bashrc, .zshrc, etc.):

cd() {
  builtin cd "$@"
  [-f.nvmrc] && nvm use
}

Now, whenever you change directories using cd and that directory contains a .nvmrc, NVM will automatically switch to the specified version.

Method 4: Using nvm install

If you are installing a new version of Node.js using NVM and you want to set it as the default immediately, you can do so by:

nvm install <version> --reinstall-packages-from=default --default

For example, to install Node.js version 20.6.1, reinstall the global npm packages from the current default version and set 20.6.1 as the new default:

nvm install 20.6.1 --reinstall-packages-from=default --default

Method 5: Using NVM GUI

If you prefer, you can also use the NVM GUI to set the default Node.js version.

The NVM GUI is a graphical user interface that makes it easy to install, manage, and use Node.js versions.

To use the NVM GUI, open the NVM GUI and select the desired Node.js version. Then, click the “Set as Default” button.

Once you have set the default Node.js version, you can verify the change by running the following command:

node -v

I hope this helps! Let me know if you have any other questions.

Leave a Comment