Installation and use of nvm (Mac version)

What is NVM?

nvm (node ​​version manager) is a tool for managing nodejs version. Sometimes our projects may depend on different versions of nodejs, and we need to switch node versions. It may be to download from the official website, overwrite the installation, and download the original version and overwrite the installation when we want to roll back... This is too troublesome. Then nvm was born to solve this problem, allowing you to switch node versions conveniently and quickly.
delete local node

First of all, you have to make sure that there are no work projects to do at present, and then do this, otherwise, if something goes wrong, this building will not be responsible.

Before installing nvm, remember the currently installed node version number, and then uninstall and delete it. (Reason: 1. Clear redundant node environment, use nvm to centrally manage nodes; 2. Avoid conflicts)

delete node

Execute the following commands in the terminal in order to delete node (reference from https://www.jianshu.com/p/920961b6a538 )

sudo npm uninstall npm -g

sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*

sudo rm -rf /usr/local/include/node /Users/$USER/.npm

sudo rm /usr/local/bin/node

sudo rm /usr/local/share/man/man1/node.1

sudo rm /usr/local/lib/dtrace/node.d

Then check:

node  //command not found

npm  //command not found

install nvm

At present, the latest version of nvm is v0.34.0. To view the latest version, please go to https://github.com/creationix/nvm

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

or

wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

After the execution is completed, nvm is stored in /.nvm in the current user directory (that is, /.nvm), and a piece of configuration code will be written into a configuration file in the current user directory ( /.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc)
insert image description here

Then we execute the command nvm, if there are operation instructions displayed, your nvm installation is successful.
insert image description here

If bash: nvm: command not found is reported, it means that there is still a problem with our environment, and the configuration file needs to be modified: open the .bash_profile file in the same directory as /.nvm (that is, ~/.bash_profile, if it does not exist, create a new one), Then add this line of code at the end of the file:

source ~/.bashrc // 上面写入配置的是.bashrc配置文件

After saving, re-open a terminal, and then enter nvm to verify, it should be fine (if you have any questions, please leave a message) nvm official website has more detailed installation tutorials https://github.com/creationix/nvm

Use of nvm

Very simple, mainly a few commands

  • nvm install stable // Install the latest stable version of node (currently the latest stable version 11.6.0)
  • nvm install // Install the specified version (install v10.15.0 or install 10.15.0)
  • nvm uninstall // Uninstall the specified version node, (if the deleted version is the current version, to unbind, execute nvm deactivate)
  • nvm use // switch to use the specified version node
  • nvm current //Display the currently used version
  • nvm ls //list all installed versions
  • nvm ls-remote //List all versions of node on the official website
  • nvm alias //Add aliases to different version numbers
  • nvm unalias //Delete a defined alias
  • nvm alias default //Specify the default version (you need to open a new terminal to take effect after setting)
  • nvm deactivate //Unbind the current version
    ...More commands can be viewed by typing nvm in the terminal
    insert image description here

*All different versions of node are installed in the ~/.nvm/version/node/ directory
*To delete and uninstall nvm, just delete the entire .nvm folder and it will be ok.

You can also refer to https://www.jianshu.com/p/622ad36ee020

Guess you like

Origin blog.csdn.net/qq_43531694/article/details/119451218