What is the .nvmrc file? How to use the .nvmrc file?

Starting from the multi-version node.js manager nvm

When we develop vue or react, the project needs to use a specified version of node.js. At this time, we can use nvm to manage multiple versions of node.js. Execute the nvm install and nvm use commands through the terminal to install and switch versions.

However, it is very troublesome to perform switching manually every time, especially when switching back and forth between multiple projects for development, it is easy to forget to switch the node version or switch to the wrong version, causing the project to run halfway and fail to start. This requires a method to manage the node.js version of the project, a method to automatically switch the node.js version, and .nvmrc should come into play!

.nvmrc

What is the .nvmrc file?

Through the above description, we probably know that the .nvmrc file is used to manage the node.js version used by our project.
This file is very simple: just the text of an nvm-recognized version of node.js. For example: v18.12.0

How to use .nvmrc file?

You can create a .nvmrcfile , the content of which is a node version number or other text that nvm can understand; details can nvm --helpbe viewed by typing in the terminal.

With the .nvmrc file, we will use the version specified by the .nvmrc file when we execute the nvm use, , nvm exec, nvm run, and nvm which commands when the terminal does not specify a version.nvm install

Create .nvmrc file

For example, in the current directory, let nvm specify the latest released version of 18.12, the latest released LTS version, and the latest released node version.
Use echo 字符串 > 指向 文件名can fill the string into the pointed file and create the file. Using the following command will create the .nvmrc file

$ echo "18.12" > .nvmrc
# 设置最新LTS版本
$ echo "lts/*" > .nvmrc 
# 设置最新版本
$ echo "node" > .nvmrc 

Execute nvm in the directory or subdirectory with the .nvmrc file

Then when you execute nvm:

$ nvm use
# 输出
Found '/code/react/todo-list/.nvmrc' with version <v18.12.0>
Now using node v18.12.0 (npm v6.14.18)

Commands such as nvm use will traverse up the directory structure from the current directory looking for the file .nvmrc. That is, running commands such as nvm use in any subdirectory of a directory with a .nvmrc, the .nvmrc will work.

The .nvmrc file must be a version number as listed <version>by nvm --help followed by a newline. Trailing whitespace is not allowed, and a trailing newline is required.

As shown in the figure below, there is no space after v14.21.3, just press enter and line feed.
.nvmrc example

Deeply integrated into the shell terminal

You can use avn to integrate deeply into your shell and automatically invoke nvm when changing directories.

If you prefer a lightweight solution, you can use the method below.

The zsh terminal automatically switches node versions according to the .nvmrc file

$HOME/.zshrcAfter adding the following code in . Automatically invoke nvm use whenever entering a directory containing a .nvmrc file,
.nvmrcthe string in the file tells nvm which version of node.js to use

# 打开 home 目录,并编辑 .zshrc 文件
$ cd
$ vi .zshrc

This is the code that needs to be written to .zshrc:

# 这一段代码复制到 .zshrc 文件中
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
    
    
  local nvmrc_path="$(nvm_find_nvmrc)"

  if [ -n "$nvmrc_path" ]; then
    local 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

After editing the .zshrc file, execute source .zshrc to make the command take effect

$ source ~/.zshrc

In the project with .nvmrc file, open the integrated terminal of vscode, you will find that nvm use has been automatically run, and the command line has corresponding output.

insert image description here

For other shells, such as bash, fish, please check the official instructions
https://github.com/nvm-sh/nvm#deeper-shell-integration

Guess you like

Origin blog.csdn.net/zhouweihua138/article/details/129762721