Since old and new projects rely on different versions of Node, you need to use NVM to manage different versions of Node and NPM.
Uninstall node and related modules
NPM ls -g --depth=0Sudo rm /usr/local/lib/node_modules sudo rm /usr/local/bin/node sudo rm /usr/local/bin/node cd /usr/local/bin && ls -l | grep".. /lib/node_modules/" | awk '{print $9}'| xargs rm
Copy the code
Install the NVM
Executing installation Commands
curl -o- https:/ / raw.githubusercontent.com/nvm-sh/nvm/v0.37.1/install.sh | bash
Copy the code
Open the terminal and go to the user’s home directory
cd ~/
Copy the code
Checking the Configuration File
Use ls -a to display all files (folders) in this directory (including hidden files and folders) and check for.zshrc
ls -a
Copy the code
Create a.zshrc file
If no, create a new one
touch ~/.zshrc
Copy the code
Configure the. ZSHRC file
To add the NVM environment variable to the shell, I use ZSH instead of bash by default. I need to configure this, open the.zshrc file, and add it on the last line
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
Copy the code
Loading a Configuration File
source ~/.zshrc
Copy the code
To install versions below 14, enter the Rosetta Shell environment first
arch -x86_64 zsh
Copy the code
Common commands
NVM ls-remote # list all installed versions NVM ls # Switch to the specified version NVM use <version> # display the version currently in use NVM current # set default`node`Version NVM aliasdefaultNVM uninstall <version>Copy the code
Node installation position
On the terminal, we can use “Which Node” to see where our node is installed. The address printed by the terminal is actually the address of the shortcut to the node version you are currently using
/Users/ Your username /.nvm/versions/node/v1013.. 0/bin/node
Copy the code
If you want to see the installation folders for all Node versions, you can view them by using the Command+Shift+G shortcut in finder and typing /Users/ your username /.nvm/versions.
But the point here is that the Mac doesn’t show hidden folders by default..nvm is a hidden folder that is not seen in the Finder. The shortcut for displaying hidden files on the Mac is Command+Shift+
So you can know that under NVM node version management mode, the installed module is not public, which means that you need to change the version after the new version of the re-install