Setting the Default Node.js Version with NVM

Introduction

Node.js is a powerful JavaScript runtime that developers use to build scalable applications. However, with new versions being released frequently, managing these versions can be difficult. This is where Node Version Manager (NVM) comes in. NVM is a command-line tool that allows you to install, update, and switch between different Node.js versions easily.

Personally, I work on a lot of different projects, each of which were either started at different times or require different Node features, so being able to switch between Node versions easily is important.

In this Byte, we'll focus on how to manage the default Node.js version with NVM.

Setting Default Node.js Version with NVM

Setting a default Node.js version with NVM is a straightforward process. First, you need to ensure that NVM is installed on your system. If it's not, you can download and install it from the official NVM GitHub repository.

Once NVM is installed, you can install a specific Node.js version by using the nvm install command followed by the version number. For example, to install Node.js version 14.15.1, you would run:

$ nvm install 14.15.1

You can also be more generic and just specify 14, for example, which would then install the latest version with the major version of 14.

After the installation, you can set this version as the default using the nvm alias default command:

$ nvm alias default 14.15.1

Now, every new terminal session will use Node.js version 14.15.1 by default.

Switching to Latest Node.js Version as Default with NVM

If you want to use the latest available version of Node.js as your default, you can do so easily with NVM. First, you need to find out the latest version. You can do this with the nvm ls-remote command, which lists all the Node.js versions available for download:

$ nvm ls-remote

Once you've identified the latest version, you can install it and set it as the default, just like we did in the previous section:

$ nvm install <latest-version>
$ nvm alias default <latest-version>

Replace <latest-version> with the actual version number.

Switching to LTS Node.js Version as Default with NVM

Long-Term Support (LTS) versions of Node.js are versions that are supported and maintained by the Node.js team for a longer period. These versions are generally more stable and recommended for production environments.

Get free courses, guided projects, and more

No spam ever. Unsubscribe anytime. Read our Privacy Policy.

To switch to the latest LTS version, you can use the nvm install --lts command, which will install the latest LTS version:

$ nvm install --lts

And then set it as the default:

$ nvm alias default --lts

Note: The --lts flag can also be used with the nvm alias default command to set the latest LTS version as the default.

This way, you can ensure that your development environment is always using a stable, long-term supported version of Node.js.

Checking Node.js Version in VS Code

In VS Code, you can easily check the version of Node.js that's currently in use. To do this, you'll need to open the terminal within VS Code. If you're not sure how to open the terminal, you can do it by clicking on View in the menu, and then selecting Terminal.

Once the terminal is open, you can check the Node.js version by entering the following command:

$ node -v

This command prompts Node.js to return the version number. The output should look something like this:

v14.15.1

The v stands for version, and the numbers following it represent the version of Node.js that is currently active. In this case, the version is 14.15.1.

Note: Remember, the version of Node.js that VS Code uses is determined by the system PATH, or by the node version set by NVM if you're using it. If you've installed multiple versions of Node.js, you can switch between them using NVM.

If for some reason you're still seeing an older version when running the above commands, try restarting VS Code.

Conclusion

Managing different versions of Node.js can be a bit tricky, especially when you're working on multiple projects that each require a different version. However, with the Node Version Manager (NVM), you can easily switch between different versions of Node.js as needed. Not only does this make it easier to manage your projects, but also helps make sure that you're always using the correct version of Node.js.

Last Updated: August 14th, 2023
Was this helpful?

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms