First of all, install nvm using brew. Give following command:
brew install nvm
Now you have to make NVM available in you bash profile using following command:
source $(brew --prefix nvm)/nvm.sh
When NVM is installed, you can install latest version of Nodejs using following command:
nvm install node
Note: this command will install latest stable version of NodeJS. To see all available nodejs versions, you can use this command:
nvm ls-remote
You can also install version specific node using following command: (here we are install node v16)
nvm install 16
Now you can see all your installed nodejs versions using this command: nvm ls
Select your desired local version of nodejs using this:
nvm use 16