Table of contents
Foreword:
Nvm is a node management tool. During project development, the node version is often too high or too low. At this time, using nvm can solve this problem very well.
nvm is a master who manages the two brothers node and npm, and npm is installed together with node. So don't worry about npm, there is no npm without node.
Before installation:
To uninstall the installed node, execute the following commands in the terminal one by one:
npm ls -g --depth=0
sudo rm -rf /usr/local/lib/node_modules
sudo rm /usr/local/bin/node
cd /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm
Install nvm:
Go to the official website to find the latest version installation command: nvm/README.md at master nvm-sh/nvm GitHub Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions - nvm/README.md at master nvm-sh/nvm https://github.com/nvm-sh/nvm/blob/master/README.md
My latest version is v0.39.1, so my latest installation command is the following.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
After executing the installation command, copy the following code to execute:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
After installation:
Execute nvm -v and the version number appears, which means the installation is successful
Use of nvm:
every time the terminal closes. None of the nvm commands can be used, prompting common not found:nvm ,
The first way is to execute the following code to ensure the normal use of nvm:
source ~/.bash_profile
The second method configures .bash_profile and .zshrc:
.bash_profile file , reopen a terminal window, enter vim ~/.bash_profile to create a .bash_profile file. Copy the code below and paste it in the created file. Then press :wq! to save and exit the editing of the current file. If there is already a .bash_profile file, then vim ~/.bash_profile cannot be created. At this time, use the open ~/.bash_profile command to open the file. Similarly, copy the following code, save it in the text, and exit.
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
After the .bash_profile file is successfully configured, execute the following code on the terminal to make the configuration take effect:
source ~/.bash_profile
.zshrc file , enter vim ~/.zshrc to create a .zshrc file. Copy the code below and paste it in the created file. Then press :wq! to save and exit the editing of the current file. If there is already a .zshrc file, then vim ~/.zshrc cannot be created. At this time, use the open ~/.zshrc command to open the file. Similarly, copy the following code, save it in the text and exit
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
After the .zshrc file is successfully configured, execute the following code on the terminal to make the configuration take effect:
source ~/.zshrc
At this time, even if the terminal is closed and opened again, nvm can be used normally.
Set up nvm Taobao mirror:
Because nvm uses a foreign mirror source by default, the download is very slow, and the download often fails. At this time, you can configure the Taobao image of nvm . The download speed will be super fast.
Execute the following command on the terminal, and the Taobao image is successfully configured:
NVM_NODEJS_ORG_MIRROR=https://npm.taobao.org/mirrors/node
Nvm common commands:
nvm ls - list all installed node versions
nvm ls-remote - list all remote server versions (official node version list)
nvm list - list all installed node versions
nvm list available - show all available versions for download
nvm install stable - install the latest version of node
nvm install [node version number] - install the specified version node
nvm uninstall [node version number] - remove the specified version installed
nvm use [node version number] - switch to the specified version node
nvm current - View the current node version
nvm alias [alias] [node version number] - add an alias to a different version number
nvm unalias [alias] - remove a defined alias
nvm alias default [node version number] - set the default version