安装nvm前先记住当前已安装的node版本号、然后将其卸载删除。(原因:1、清除多余的node环境,用nvm集中管理node;2、避免出现冲突)
依次在终端执行以下命令,删除node
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
然后检查下:
node //command not found
npm //command not found
Git Install
If you have git installed (requires git v1.7.10+):
clone this repo in the root of your user profile
cd ~/ from anywhere then git clone https://github.com/nvm-sh/nvm.git .nvm
cd ~/.nvm and check out the latest version with git checkout v0.39.0
activate nvm by sourcing it from your shell: . ./nvm.sh
Now add these lines to your ~/.bashrc, ~/.profile, or ~/.zshrc file to have it automatically sourced upon login: (you may have to add to more than one of the above files)
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
安装nvm方法官方git地址:https://github.com/nvm-sh/nvm
使用 cnpm 加速 npm
npm install -g cnpm --registry=http://registry.npmmirror.com
nvm常用命令
nvm install <version> ## 安装指定版本,可模糊安装,如:安装v4.4.0,既可nvm install v4.4.0,又可nvm install 4.4
nvm uninstall <version> ## 删除已安装的指定版本,语法与install类似
nvm use <version> ## 切换使用指定的版本node
nvm ls ## 列出所有安装的版本
nvm ls-remote ## 列出所以远程服务器的版本(官方node version list)
nvm current ## 显示当前的版本
nvm alias <name> <version> ## 给不同的版本号添加别名
nvm unalias <name> ## 删除已定义的别名
nvm reinstall-packages <version> ## 在当前版本node环境下,重新全局安装指定版本号的npm包
附:MAC 打开.bash_profile
启动终端Terminal
进入当前用户的home目录
输入cd ~
创建.bash_profile
输入touch .bash_profile
编辑.bash_profile文件
输入open -e .bash_profile
保存文件,关闭.bash_profile
更新刚配置的环境变量
输入source .bash_profile