How to Write an Nvmrc File for Automatic Node Version Change

Avatar

By squashlabs, Last Updated: Oct. 1, 2023

How to Write an Nvmrc File for Automatic Node Version Change

An .nvmrc file is a simple text file that specifies a specific version of Node.js to be used for a project. This file is used by the Node Version Manager (NVM) tool to automatically switch to the specified Node.js version when you navigate to the project directory.

Step 1: Create a new file

To create an .nvmrc file, navigate to the root directory of your project using a terminal or command prompt. Then, create a new file with the name .nvmrc (including the dot at the beginning of the file name).

Related Article: How to Install a Specific Version of an NPM Package

Step 2: Specify the Node.js version

Open the .nvmrc file in a text editor and specify the desired Node.js version. The version should be in the format major.minor.patch. For example, if you want to use Node.js version 14.17.0, simply write 14.17.0 in the file. Make sure there are no leading or trailing spaces in the file.

Step 3: Save the file

Save the .nvmrc file after specifying the Node.js version. Make sure to save the file in the root directory of your project.

Step 4: Automatic version switching

Once you have created and saved the .nvmrc file, the NVM tool will automatically switch to the specified Node.js version whenever you navigate to the project directory using the terminal or command prompt.

Related Article: How to Uninstall npm Modules in Node.js

Best Practices and Considerations

- It is recommended to include the .nvmrc file in your project's version control system (such as Git) to ensure that all team members are using the same Node.js version.

- When creating an .nvmrc file, it's a good practice to use the latest LTS (Long Term Support) version of Node.js. LTS versions are typically more stable and receive long-term support from the Node.js community.

- If you want to use the latest available Node.js version installed on your system, you can specify lts in the .nvmrc file. NVM will automatically use the latest LTS version.

- It is also possible to specify a range of Node.js versions in the .nvmrc file. For example, you can specify >=12.0.0 <14.0.0 to use any version between 12.0.0 (inclusive) and 14.0.0 (exclusive).

- If the specified Node.js version is not installed on your system, NVM will display an error message when you navigate to the project directory. You can then use the NVM tool to install the required version.

Example

Suppose you are working on a project that requires Node.js version 14.17.0. Here's how you can create an .nvmrc file for automatic version switching:

1. Open a terminal or command prompt.

2. Navigate to the root directory of your project.

3. Create a new file named .nvmrc.

4. Open the .nvmrc file in a text editor.

5. Write 14.17.0 in the file (without any leading or trailing spaces).

6. Save the .nvmrc file.

7. Close the text editor.

8. From the terminal or command prompt, navigate to the project directory.

9. NVM will automatically switch to Node.js version 14.17.0.

You May Also Like

How to Resolve the Npm Warnings with Config Global & Local

Node.js developers often encounter warnings related to the deprecated npm config global "--global" and "--local" flags. These warnings can be confusi… read more

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. Di… read more

Integrating HTMX with Javascript Frameworks

Integrating HTMX with JavaScript frameworks is a valuable skill for frontend developers. This article provides best practices for using HTMX with pop… read more

Building a Language Learning Game with GraphQL & Nodejs

This tutorial teaches readers how to create a language learning game using GraphQL, Node.js, React.js, MongoDB, and Docker. The article covers topics… 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 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 Update Node.Js

Node.js is an essential tool for many developers, and keeping it up to date is crucial for a smooth development process. In this article, you will le… 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 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

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 … read more