How to Set the Default Node Version Using Nvm

Avatar

By squashlabs, Last Updated: September 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: How To Update Node.Js

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: How To Check Node.Js Version On Command Line

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

Advanced DB Queries with Nodejs, Sequelize & Knex.js

Learn how to set up advanced database queries and optimize indexing in MySQL, PostgreSQL, MongoDB, and Elasticsearch using JavaScript and Node.js. Discover query... read more

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

Node.js is a powerful platform for building scalable and web applications. In this article, we will explore advanced features of Node.js such as the Event Loop, Async... 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 explores the role of... read more

Big Data Processing with Node.js and Apache Kafka

Handling large datasets and processing real-time data are critical challenges in today's data-driven world. In this article, we delve into the power of Node.js and... read more

Build a Movie Search App with GraphQL, Node & TypeScript

Building a web app using GraphQL, Node.js, and TypeScript within Docker? Learn how with this article. From setting up MongoDB to deploying with Docker, building the... read more

Build a Virtual Art Gallery with Reactjs, GraphQL & MongoDB

This article teaches you how to build a virtual art gallery app using React.js, GraphQL, MongoDB, and Docker. You will learn about additional resources, project... read more