mac中nvm管理node


nvm安装之前若是电脑上存在node,要先将node卸载掉

检查电脑上是否存在node
node -v
  • 若是显示版本号 如v16.18.0 则表示当前电脑存在node
  • 若是显示 command not found: node 则表示当前电脑不存在node

若是node存在需要先将电脑中的node卸载

卸载node
sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

卸载完成之后再使用命令 node -v 检查一下是否卸载成功,卸载成功之后就可以安装nvm了

安装nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

使用上述两个命令中的任意一个命令都可以安装 自动化安装nvm(不需要进行配置)

报错
port 443: Connection refused  // 拒绝链接
  • 报错原因:
    • github 的一些域名的 DNS 解析被污染,导致DNS 解析过程无法通过域名取得正确的IP地址
  • 解决方法
    • 在本机host文件添加github 域名 与 对应ip地址 的映射关系
        199.232.68.133 raw.githubusercontent.com
        199.232.68.133 user-images.githubusercontent.com
        199.232.68.133 avatars2.githubusercontent.com
        199.232.68.133 avatars1.githubusercontent.com
      
查看本机的ip地址
  ifconfig

然后en0对应的就是电脑的ip地址

修改host文件
  • 打开host文件

      vi /etc/hosts
    

    此时文件不可编辑输入i 更改为编辑

  • 编辑host文件

     i
    

    输入i之后会显示如下字样,表示可编辑
    在这里插入图片描述

  • 编辑完成之后 按esc退出编辑

  • 保存

     :wq
    
    • 报错

       'readonly' option is set (add ! to override)
      
      • 原因: 没有权限
      • 解决:先获取权限,再进行编辑
           Sudo vim /etc/hosts
        
        输入上述命令之后,会显示如下选择 -> 选择自己所需要的权限
        在这里插入图片描述
        选择E -> 进行编辑
      • 再修改就可以正常保存了
重新安装nvm - 报错

执行如下命令重新安装nvm

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

报了如下错误:

curl: (35) LibreSSL SSL_connect: SSL_ERROR_SYSCALL in connection to raw.githubusercontent.com:443 

是因为你配置了代理,需要移除 -> 使用下述命令移除代理

git config --global --unset http.proxy
重新安装nvm - 报错

执行如下命令重新安装nvm

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

此时发现可以正常下载了

weiche@liujuncheng-macbookair2 ~ % curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15037  100 15037    0     0   9583      0  0:00:01  0:00:01 --:--:--  9577
=> Downloading nvm from git to '/Users/weiche/.nvm'
=> Cloning into '/Users/weiche/.nvm'...
remote: Enumerating objects: 357, done.
remote: Counting objects: 100% (357/357), done.
remote: Compressing objects: 100% (303/303), done.
remote: Total 357 (delta 39), reused 173 (delta 28), pack-reused 0
Receiving objects: 100% (357/357), 218.03 KiB | 283.00 KiB/s, done.
Resolving deltas: 100% (39/39), done.
* (HEAD detached at FETCH_HEAD)
  master
=> Compressing and cleaning up git repository

=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
   OR
=> Append the following lines to the correct file yourself:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

=> Close and reopen your terminal to start using nvm or run the following to use it now:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
weiche@liujuncheng-macbookair2 ~ % 

但是还是报了一个错:

Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
Create one of them and run this script again
  • 报错原因:显示是找不到对应的文件
  • 解决:新建个bash_profile文件
      cd // 进入系统根目录
      touch .dash_profile // 创建dash_profile文件
      open .dash_profile // 打开dash_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
    
安装完成

到上一步安装就完成了,此时执行命令nvm -v检查一下是否安装成功,发现是command not found;

原因是因为安装完成之后一定要退出终端重新打开才能生效,退出打开 -> 此时显示对应的版本号了。

兼容性

下载nvm的原因是因为现在接手的这个项目的node需要使用12.13版本的。

现在 这个项目可以正常运行了

运行之前的项目 -> 报错
A complete log of this run can be found in
  • 原因1: 没有当前命令
运行之前的项目 -> 报错
Failed to load plugin 'flowtype' declared in 'package.json » eslint-config-react-app': Cannot find module 'eslint/use-at-your-own-risk'
  • 原因:node版本过低
  • 解决:低于16请升级至16,我用的是16.14.2版本
常用命令
  • 查看nvm 版本
     nvm -v
    
  • 查看node版本列表
      nvm ls
    
    在没有node的情况下,会有如下提示
    在这里插入图片描述
    此时需要下载 node -> 下载12.13版本的node
    nvm install 12.13
    
    再次执行命令 nvm ls
    在这里插入图片描述
  • 卸载node指定版本
    nvm uninstall node 版本号
    
  • 使用node指定版本
     nvm use node 版本号
    
  • 查看当前node使用版本
      nvm current
    
  • 设置默认版本号
    nvm alias default 版本号
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值