一、安装nvm
终端输入:
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 配置文件
①法一:直接在终端编辑(:wq 保存并退出):
vi ~/.zshrc
②法二:找到对应的文件夹(更方便)
在 ~/.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 命令使配置生效
source ~/.zshrc
查看配置是否生效
echo $NVM_DIR
查看帮助手册
nvm -help
Note:
to remove, delete, or uninstall nvm – just remove the `$NVM_DIR` folder (usually `~/.nvm`)
删除卸载nvm只需要移除$NVM_DIR文件夹,一般是~/.nvm
查看版本
nvm -v
nvm ls-remote // 查看所有的node可用版本
二、安装node
安装指定版本的node
nvm install 版本号
node -v
npm -v
淘宝镜像源
npm config set registry https://registry.npm.taobao.org