Join my Laravel for REST API's course on Udemy šŸ‘€

How to generate an .nvmrc file with the current Node.js version

November 18, 2021 Ā ā€Ā 2Ā min read

In the .nvmrc file you may specify the Node.js version you would prefer to use in your project. Normally you create this file in your project's root directory. The Node Version Manager (nvm) respects the version set in this file. Meaning the nvm subcommands use, install, exec, run, etc. will use the version that is set in the .nvmrc file.

In order to set your current Node.js version in the .nvmrc file you may run the following command.

node -v > .nvmrc

The line above makes use of the >-redirection operator of your shell to write the standard output of the node -v command to the .nvmrc file. If the file doesnā€™t exist yet it will be created. If it did exist however, the contents of the file will be overwritten.

It is important to take into account that nvm does not change your Node.js version automatically once you are located in a directory containing a .nvmrc file, the subcommands of nvm just take this file into account. So you still need to run nvm use in order to set the right version of Node.js using nvm. The nvm docs do show you how you can automate this step for the shell you are using.

Use the latest Node LTS version

Besides setting an exact version in your .nvmrc file there are other alternatives too. You may set your Node.js version to the latest long term support (lts) version for example:

echo "lts/*" > .nvmrc

Use the latest Node version

Another option would be to default to the latest available option, you do this by just setting the contents of the .nvmrc file to ā€œnodeā€:

echo "node" > .nvmrc