NVM用于在命令行中安装和切换不同版本的node(npm)。本文介绍如何在MacOS中使用homebrew安装NVM。
前提
安装HomeBrew, 国内用户参考
第一步 移除已安装的node
如果你的系统中已经安装node,首先将期卸载
brew uninstall --ignore-dependencies node
brew uninstall --force node
第二步 安装NVM
更新homebrew包,并安装nvm
brew update
brew install nvm
安装成功后,会观察到如下提示:
You should create NVM's working directory if it doesn't exist:
mkdir ~/.nvm
Add the following to ~/.zshrc or your desired shell
configuration file:
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
You can set $NVM_DIR to any location, but leaving it unchanged from
/opt/homebrew/opt/nvm will destroy any nvm-installed Node installations
upon upgrade/reinstall.
Type `nvm help` for further information.
根据提示,创建一个目录作为NVM的主目录
mkdir ~/.nvm
将如下内容写入到~/.zshrc中
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
并在当前命令行环境中进行加载
source ~/.zshrc
至此NVM已经安装完成
第三步 使用NVM安装node
可使用如下命令查看可以安装的node的版本:
nvm ls-remote
你可以安装输出列表中的任意版,比如,我使用如下命令安装了当前的长期维护版本
nvm install v16.13.0
安装成功后,可以通如下命令验证安装结果和当前使用版本
nvm ls
截屏
如果你安装了多个版本,可以随时使用如下命令进行版本切换
nvm use 14
结论
本文介绍了如何在macOS上安装NVM和node
网友评论