How to use multiple node js versions
If you currently working on multiple projects on the same machine and every project should use a different node version, in this case, you might uninstall and reinstall a new version every time you start work on a different project which is a ridiculous situation.
The solution
Node Version Manager https://github.com/nvm-sh/nvm in this link you can find the instructions to install it on different operating systems.
After installing NVM on your machine and install different versions of node js
still, you have an issue that every time you switch between projects you need to run this command nvm use (the version number)
ex: nvm use 14.0.0
but it is still annoying, so we can do the following steps to switch automatically between versions:
1- add a new file at the root of your application with this name .nvmrc
and put the node version that you use in this project for example if you use v14.0.0
then please add the version number without v
2- open your terminal configurations for example if you use zsh
then open the configuration on this path ~/.zshrc
and copy & paste this shell script
# place this after nvm initialization!autoload -U add-zsh-hookload-nvmrc() {if [[ -f .nvmrc && -r .nvmrc ]]; thennvm useelif [[ $(nvm version) != $(nvm version default) ]]; thenecho "Reverting to nvm default version"nvm use defaultfi}add-zsh-hook chpwd load-nvmrcload-nvmrc
3- Close all the terminal windows and open it again on your project root folder and just it, and your terminal will switch automatically to the node version that you have in the .nvmrc
file that is at the root of your project.
Happy learning :)