mac安装nvm

系统:macos catalina
版本:10.15.7

一.安装nvm

打开终端执行这个命令 安装的版本是 v0.39.1

curl -o- [https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh](https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh) | bash

二.查看版本

nvm --version

三.使用nvm安装node

# 安装node
nvm install node #安装最新的稳定版本
nvm install 14.20.0 #安装指定版本

# 切换node版本
nvm use node #默认版本
nvm use 14.20.0 #指定版本

如果安装过程中报错443,那么需要手动配置hosts文件

配置hosts文件
1. 打开访达,快捷键Shift+Command+G查找文件,输入/etc/hosts,点击前往。或者在终端Terminal中 输入 open /etc/hosts 再Enter就打开文件了,

2.复制hosts文件到桌面,打开文件后在最后一行添加

185.199.108.133 raw.Githubusercontent.com


修改好以后替换 /etc/hosts 路径下面的原有文件即可,会要求输入电脑密码。

如果安装后不能使用nvm提示:zsh: command not found: nvm


解决方法配置 .bash_profile和 .zshrc

如果你的mac两个文件都没有那么要自己创建,

.bash_profile中修改的环境变量只对当前窗口有效,修改完.bash_profile之后记得在终端输入source ~/.bash_profile使之生效。
.zshrc在电脑每次自启时都会生效(永久有效),修改完之后在终端输入source ~/.zshrc使之生效。
# 打开文件,提示没有这个文件
mac@Mac ~ % open ~/.bash_profile
The file /Users/imqd/.bash_profile does not exist.
# 创建 .bash_profile文件:
mac@Mac ~ % cd ~/
mac@Mac ~ % touch .bash_profile
# 打开 .bash_profile文件
mac@Mac ~ % open ~/.bash_profile
  ## 在 .bash_profile文件中添加以下内容
  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

# 打开.zshrc,如果没有则用同样的命令创建.zshrc并打开它
# 创建 .bash_profile文件:
mac@Mac ~ % cd ~/
mac@Mac ~ % touch .bash_profile
# 打开 .bash_profile文件
mac@Mac ~ % open ~/.bash_profile
  ## 在这个文件中最后新增一行
  source ~/.bash_profile

配置好以后退出terminal重新进入查看nvm版本即可

posted @ 2023-09-10 13:08  yangchin9  阅读(714)  评论(0编辑  收藏  举报