How to Set the Default Node Version Using Nvm

Avatar

By squashlabs, Last Updated: Sept. 16, 2023

How to Set the Default Node Version Using Nvm

Introduction

Setting the default Node version using nvm (Node Version Manager) allows you to easily switch between different versions of Node.js on your system. This can be useful when working on projects that require different Node versions or when you want to test your code against different Node versions. In this guide, we will walk through the steps to set the default Node version using nvm.

Related Article: Advanced Node.js: Event Loop, Async, Buffer, Stream & More

Step 1: Install nvm

To set the default Node version using nvm, you first need to have nvm installed on your system. If you haven't installed nvm yet, follow the official installation instructions for your operating system:

- For Linux/OS X: https://github.com/nvm-sh/nvm#installation-and-update

- For Windows: https://github.com/coreybutler/nvm-windows#installation--upgrades

Make sure to choose the installation method that is appropriate for your operating system.

Step 2: List Available Node Versions

Once nvm is installed, you can list the available Node versions on your system by running the following command in your terminal:

nvm ls-remote

This will display a list of all the Node versions that are available for installation using nvm. Take note of the version number that you want to set as the default.

Step 3: Install the Desired Node Version

To install a specific Node version using nvm, you can run the following command in your terminal, replacing <version> with the actual version number you want to install:

nvm install <version>

For example, to install Node.js version 14.17.1, you would run:

nvm install 14.17.1

Wait for the installation process to complete. Once the installation is finished, you can verify that the Node version has been installed by running:

node --version

This should display the version number of the Node installation.

Related Article: Fix The Engine Node Is Incompatible With This Module Nodejs

Step 4: Set the Default Node Version

To set the default Node version to be used by nvm, you can run the following command in your terminal, replacing <version> with the version number you want to set as the default:

nvm alias default <version>

For example, to set Node.js version 14.17.1 as the default, you would run:

nvm alias default 14.17.1

Now, whenever you open a new terminal session or restart your computer, the default Node version will be automatically set to the version you specified.

Step 5: Verify the Default Node Version

To verify that the default Node version has been set correctly, you can run the following command in your terminal:

node --version

This should display the version number of the default Node installation that you set.

Alternative Method: Using .nvmrc File

Another way to set the default Node version for a specific project is by using an .nvmrc file. This file should be placed in the root directory of your project and contain the desired Node version number.

To set the default Node version for a project using an .nvmrc file, follow these steps:

1. Create a file named .nvmrc in the root directory of your project.

2. Open the .nvmrc file in a text editor and enter the desired Node version number (e.g., 14.17.1).

3. Save the file.

Now, whenever you navigate to the project directory using the terminal, nvm will automatically switch to the Node version specified in the .nvmrc file.

You May Also Like

How to Fix “Npm Err! Code Elifecycle” in Node.js

A quick guide to fix the "Npm Err! Code Elifecycle" error in Node.js applications. Learn how to resolve the issue by checking your package.json file,… read more

How to Solve ‘Cannot Find Module’ Error in Node.js

This article provides a clear guide for resolving the 'Cannot Find Module' error in Node.js. It covers possible solutions such as checking the module… read more

How to Run 100 Queries Simultaneously in Nodejs & PostgreSQL

Learn how to execute 100 queries simultaneously in Node.js with PostgreSQL. Understand how PostgreSQL handles executing multiple queries at once and … read more

Implementing i18n and l10n in Your Node.js Apps

Internationalization (i18n) and localization (l10n) are crucial aspects of developing Node.js apps. This article explores the process of implementing… read more

How to Read a File in Node.js

Reading files in Node.js can be made easy with the fs module. This guide will cover different methods, best practices, and alternative approaches to … read more

How to Install a Specific Version of an NPM Package

Installing a specific version of an NPM package in a Node.js environment can be a process. By following a few steps, you can ensure that you have the… read more

How to Fix “Connect ECONNREFUSED” Error in Nodejs

A simple guide to solve the common Node.js error: Connect ECONNREFUSED. Learn how to fix this error in Node.js by checking server configuration, netw… read more

How to Fix the “getaddrinfo ENOTFOUND” Error in Node.js

Simple steps to resolve the getaddrinfo ENOTFOUND error in Node.js. Check the hostname or domain name, verify network connectivity, check DNS configu… read more

AI Implementations in Node.js with TensorFlow.js and NLP

This article provides a detailed look at using TensorFlow.js and open-source libraries to implement AI functionalities in Node.js. The article explor… read more

How to Fix “nvm command not found” with Node Version Manager

A guide on fixing the "nvm command not found" issue while installing Node Version Manager. Learn how to update your shell configuration, reinstall No… read more