How to Write an Nvmrc File for Automatic Node Version Change

Avatar

By squashlabs, Last Updated: October 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 Run 100 Queries Simultaneously in Nodejs & PostgreSQL

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 Upgrade Node.js To The Latest Version

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 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 the impact it can... read more

How to Git Ignore Node Modules Folder Globally

Setting up Git to ignore node_modules folders globally can greatly simplify your development workflow. This article provides a simple guide on how to achieve this,... read more

How To Check Node.Js Version On Command Line

Checking the Node.js version on the command line is an essential skill for any Node.js developer. In this article, you will learn various methods to quickly determine... read more

How to Differentiate Between Tilde and Caret in Package.json

Distinguishing between the tilde (~) and caret (^) symbols in the package.json file is essential for Node.js developers. This article provides a simple guide to... read more

How to Use Force and Legacy Peer Deps in Npm

A simple guide on using force and legacy peer deps features in Npm within Node.js context. Learn how to utilize the force flag and the legacy peer deps flag effectively.... read more