Logo

How can the default node version be set using NVM?

Node Version Manager (NVM) allows you to install and switch between multiple Node.js versions easily. If you want a particular Node version to be the default whenever you open a new shell session or terminal, you can use nvm alias to set it. Below are the main steps:

1. Install or Update NVM (If Needed)

If you don’t have nvm installed:

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

Check the NVM GitHub repo for the latest version and instructions.

2. Install Your Desired Node Version

For example, to install the latest LTS release:

nvm install --lts

Or install a specific version (e.g., 18.16.0):

nvm install 18.16.0

3. Set the Default Version with nvm alias

To make a certain version the default, use nvm alias default <version>.

Example A – Using the “latest” keyword:

nvm install node # Installs the latest current version nvm alias default node

Example B – Using a specific version:

nvm alias default 18.16.0

This tells nvm to use Node.js 18.16.0 automatically whenever you open a new terminal session.

4. Verify Your Default Version

Close and reopen your terminal (or run source ~/.nvm/nvm.sh) and then check the active Node version:

node -v

It should reflect whichever version you aliased as default.

5. Additional Tips

  1. Listing Installed Versions
    nvm ls
    Shows all Node versions installed on your system and indicates which one is currently in use.
  2. Switching Temporarily
    nvm use <version>
    Lets you switch to a different version in the current session without changing the default.
  3. Upgrading
    If a new Node version is released, simply install it (nvm install <version>) and re-alias if you want that to become your new default.

With these steps, you’ll have a consistent Node.js version each time you open your terminal, ensuring smooth development workflows across different projects and sessions.

Recommended Resource

CONTRIBUTOR
TechGrind