一、简介
nvm 的全称是 Node Version Manager,是一个 Node.js 的版本管理工具。
二、安装步骤
1、卸载电脑上已安装的node.js(可选)
方法1:
sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}方法2:
#卸载npm
sudo npm uninstall npm -g
#若上面的uninstall npm 下载不了,进入npm所在的文件夹
cd /usr/local/lib/node_modules/npm
#卸载npm
sudo make uninstall
#卸载node.js
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.d2、 安装编译依赖(可选) 对于 Linux,macOS 通常不需要。对于基于 Debian/Ubuntu 的系统,可能需要:
sudo apt-get update
sudo apt-get install build-essential libssl-dev3、下载并安装 nvm
sudo curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash下载报错解决方案:echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null
4、重新加载 Shell 配置文件
安装脚本会将 NVM 的路径添加到你的 shell 配置文件中(如 .bash_profile、.zshrc 或 .profile)。你需要重新加载这个文件使 NVM 生效。
#bash 用户:
source ~/.bash_profile
#zsh 用户:
source ~/.zshrc5、验证
nvm --version三、nvm基础命令
1、安装特定版本的 Node.js
nvm install 14.17.02、切换到特定版本的 Node.js
nvm use 14.17.0
nvm use 14.21.33、设置npm镜像源
npm config set registry https://registry.npmmirror.com/4、列出已安装的 Node.js 版本
nvm ls5、列出所有可安装的 Node.js 版本
nvm ls-remote6、设置默认 Node.js 版本 你可以设置一个默认的 Node.js 版本,当打开新的终端会话时会自动使用该版本。
nvm alias default 14.17.07、显示当前正在使用的 Node.js 版本
nvm current8、卸载指定版本的 Node.js
nvm uninstall <version>