如何编写.nvmrc实现Node版本自动切换及Angular项目自动切换Node版本
Got it, let's cover two reliable methods to automate Node version switching between your AngularJS 4.4.7 and Angular 6 projects—one using NVM's built-in .nvmrc (since you already use NVM, this is the most straightforward) and another with avn and .node-version.
Method 1: Use .nvmrc with NVM (No Extra Tools Needed)
NVM has native support for a .nvmrc file to auto-switch versions when you enter a project directory. Here's how to set it up:
Confirm required Node versions for each project
- AngularJS 4.4.7 requires Node 6.9.0+; we recommend Node 8.x for stability (e.g.,
v8.17.0). - Angular 6 requires Node 8.9.0+; Node 10.x works well (e.g.,
v10.24.1). - Manually switch to each version first with
nvm use <version>and verify withnode -v.
- AngularJS 4.4.7 requires Node 6.9.0+; we recommend Node 8.x for stability (e.g.,
Create the
.nvmrcfile in each project root- Open your terminal, navigate to your AngularJS project root, and run:
echo "v8.17.0" > .nvmrc - Repeat for your Angular 6 project, replacing the version with your target (e.g.,
echo "v10.24.1" > .nvmrc). - Alternatively, manually create a plain text file named
.nvmrcin the project root, and paste just the version number (with or without thevprefix—NVM recognizes both).
- Open your terminal, navigate to your AngularJS project root, and run:
Configure your terminal to auto-load the version
- For Bash: Open your
~/.bashrcfile and add this snippet:cd() { builtin cd "$@" if [ -f .nvmrc ]; then nvm use fi } - For Zsh: Open your
~/.zshrcfile and add the same snippet. - Save the file, then restart your terminal or run
source ~/.bashrc(orsource ~/.zshrc) to apply the changes.
- For Bash: Open your
Test it out
- Navigate to either project directory—your terminal will automatically run
nvm useand switch to the version specified in.nvmrc. You’ll see a confirmation message likeNow using node v8.17.0 (npm v6.14.13).
- Navigate to either project directory—your terminal will automatically run
Method 2: Use avn with .node-version
If you prefer avn, here's how to set it up with .node-version files:
Install avn and its NVM integration
- Run this command to install avn globally:
npm install -g avn avn-nvm - Initialize avn by running:
avn setup - Follow the prompts to update your shell configuration file (Bash/Zsh). This ensures avn runs automatically when you open your terminal.
- Run this command to install avn globally:
Create the
.node-versionfile in each project root- Navigate to your AngularJS project root and run:
echo "8.17.0" > .node-version - Note: Unlike
.nvmrc, avn uses the version number without thevprefix. - Repeat for your Angular 6 project with your target version (e.g.,
echo "10.24.1" > .node-version). - You can also manually create the file and paste just the version number.
- Navigate to your AngularJS project root and run:
Test the auto-switch
- Restart your terminal, then navigate to either project directory. avn will automatically detect the
.node-versionfile and tell NVM to switch to the specified version. Verify withnode -v.
- Restart your terminal, then navigate to either project directory. avn will automatically detect the
Important Notes
- Pre-install Node versions: Make sure you’ve already installed the required Node versions via NVM (e.g.,
nvm install 8.17.0) before setting up auto-switching—otherwise, you’ll get an error saying the version isn’t installed. - Stick to Angular’s official requirements: Always cross-check the Node version compatibility for your Angular/AngularJS version to avoid build or runtime issues.
内容的提问来源于stack exchange,提问作者Janani Hariharan

