Change project, Change node version let .nvmrc help you

Thawinwats
3 min readOct 24, 2023

--

When working on multiple Node.js projects, managing different Node versions can become a daunting task. Fortunately, there’s a handy tool that can simplify this process and save you from version-related hassles: the .nvmrc file.

Node Version Manager (NVM) is a popular tool used by developers to switch between different Node.js versions. However, remembering the required Node version for each project can be cumbersome. This is where the .nvmrc file comes to the rescue.

Example to use from nvm GitHub:

$ nvm use 16
Now using node v16.9.1 (npm v7.21.1)
$ node -v
v16.9.1
$ nvm use 14
Now using node v14.18.0 (npm v6.14.15)
$ node -v
v14.18.0
$ nvm install 12
Now using node v12.22.6 (npm v6.14.5)
$ node -v
v12.22.6

What is .nvmrc?

.nvmrc is a simple text file that specifies the Node.js version a project requires. By creating a .nvmrc file in your project's directory, you can instruct NVM to switch to the appropriate Node version whenever you navigate to that directory.

Why Use .nvmrc?

  1. Version Consistency: Ensuring that each project uses the correct Node.js version is crucial for compatibility and stability. .nvmrc provides a straightforward way to maintain version consistency.
  2. Ease of Use: Managing Node versions on a project-by-project basis becomes effortless. You don’t have to remember or look up which version is needed for each project.
  3. Collaboration: When collaborating with others on a project, sharing the .nvmrc file ensures that everyone is using the same Node.js version, reducing potential conflicts and issues.

Creating and Using .nvmrc

Creating and using an .nvmrc file is a straightforward process:

  1. Navigate to your project directory.
  2. Create a file named .nvmrc in the project's root directory.
  3. Inside the .nvmrc file, simply write 12.

Now, every time you enter your project directory, NVM will automatically set the Node version to 12 for that project.

Calling nvm use Automatically in a Directory

To enable automatic Node version switching when you enter a directory with an .nvmrc file, follow these steps on Deeper Shell Integration

Example with zsh:

Put this into your $HOME/.zshrc to call nvm use automatically whenever you enter a directory that contains an .nvmrc file with a string telling nvm which node to use:

# place this after nvm initialization!
autoload -U add-zsh-hook

load-nvmrc() {
local nvmrc_path
nvmrc_path="$(nvm_find_nvmrc)"

if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version
nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")

if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}

add-zsh-hook chpwd load-nvmrc
load-nvmrc

Additional Tips:

  • Version Ranges: You can specify version ranges in your .nvmrc file. For example, >=14.0 <15.0 will allow any Node version in the 14.x range.
  • Check Node Version: To check which Node version is currently in use in a directory, use the nvm current command.
  • Global Node Version: .nvmrc is particularly useful for per-project Node version management, but you can still use NVM globally to set your default Node version.

In conclusion, the .nvmrc file is a simple yet powerful tool that can streamline your Node.js development process. It ensures version consistency, simplifies collaboration, and makes managing Node versions project-specific a breeze. Incorporating .nvmrc into your workflow can save you time and headaches, allowing you to focus on what matters most: writing code.

So, the next time you’re working on a Node.js project, let .nvmrc be your guide to effortlessly manage Node versions. Say goodbye to version-related issues, and welcome a more efficient and organized development experience.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Thawinwats
Thawinwats

No responses yet

Write a response