Managing Multiple Node Versions

I have a few projects on my local machine which require specific versions of Node. I learned today how I can have multiple versions of Node installed and automatically configure a project to use a specific version when I switch to its directory.
Install Node Version Manager (NVM)
After installing, restart the terminal or source your profile script to ensure NVM is loaded:
Install multiple versions of Node
To manually switch between multiple versions of Node, run nvm use
:
To automate Node version switching for specific projects
First, place a .nvmrc
file in the root of the project directory with the required Node version noted inside:
Then, to automate the process of switching Node versions based on the .nvmrc
file when you cd
into that root directory, you can add the following function to your shell's configuration file (.bashrc, .bash_profile, .zshrc, etc.):
This script does the following:
- Checks if there's an
.nvmrc
file in the current directory. - If there is, it switches to the Node version specified in that file.
- Upon leaving the directory, it switches back to the default Node version.
Apply Changes to your shell configuration