NVM Guide: Set Default Node Version

Updated

Follow this guide to set the default node version in NVM and also download multiple node version.

NVM Guide

Maintaining and easily alternating between multiple Node versions in your development environment is vitally important. The NVM package trivializes this process, so here are some useful commands to help you set default node versions, install new node versions, and more. 

Setting the Default Node Version in NVM

To set a default node version in NVM, use the alias default command.

nvm alias default <version>
      
    

Here is a full example of where you would first install the node version you want and then set it as the default version. We will install node version 16.17.0

nvm install 16.17.0
nvm alias default 16.17.0
nvm use default

You can use the nvm ls command to verify which node version is the current default. This command will also show you all the node versions you have installed through the Node Version Manager.

Installing Different Node Versions in NVM

Before settings a specific node version as your primary default, you will likely need to download a version of Node using NVM. But which one to download? This will vary from project to project, but the latest long-term support (LTS) version of Node is a good place to start. 

nvm install --lts
      
    

The command above will download and install the current LTS version of Node. This version is guaranteed to be maintained until a specified date. You can find the current Node release schedule here. This schedule includes the version, initial release date, active LTS start date, and end-of-life date. 

If you want a more detailed list of previous node versions, you can check out the official Node releases, which provide changelogs and documentation. 

Why Would You Need Multiple Node Versions?

One reason to have multiple Node versions installed in your development environment is to maintain close parity with your production environment. Your production and development environments should be nearly identical to help prevent potentially catastrophic bugs. 

Imagine that you have to write an additional function or module to a codebase already in production. If the production environment is running in Node v14, but you are developing in Node v18, some of your code may not correctly execute. Of course, it would make little sense to roll back your Node version in your development environment completely, so a package like NVM comes in handy. You can install both Node v14 and Node v18 on your machine and alternate between them as necessary. 

Other Helpful NVM Commands

You can always type nvm --help to get a comprehensive list of all the NVM commands. Here are some of the most popular ones:

  • nvm ls – List installed node versions
  • nvm install <version> – Install a specific version of Node
  • nvm use <version> – Change to the specific version of Node
  • nvm run <version> <script> – Run node script with specific version
  • nvm current – Show the currently active version of Node

We hope you found this guide helpful. Please check out our Javascript Coding Section for more useful tips, tricks, and guides.