mac电脑安装 nvm 报错如何解决

前言

已知:安装nvm成功;终端输入nvm -v 有版本返回

1. 启动全局配置环境变量失败

bash 复制代码
source ~/.zshrc~ 

返回:

bash 复制代码
source: no such file or directory: /Users/你的用户名/.zshrc~

2 安装node失败

bash 复制代码
nvm install 16.13

返回:

bash 复制代码
mkdir: /Users/你的用户名/.nvm/alias: Permission denied
mkdir: /Users/你的用户名/.nvm/alias: Permission denied
Downloading and installing node v16.13.2...
mkdir: /Users/你的用户名/.nvm/.cache: Permission denied
creating directory /Users/你的用户名/.nvm/.cache/bin/node-v16.13.2-darwin-arm64/files failed
Downloading https://nodejs.org/dist/v16.13.2/node-v16.13.2-darwin-arm64.tar.xz...
Warning: Failed to create the file                                             
Warning: /Users/你的用户名/.nvm/.cache/bin/node-v16.13.2-darwin-arm64/node-v16.13.
Warning: 2-darwin-arm64.tar.xz: No such file or directory
                                                                            0.0%curl: (23) Failure writing output to destination

download from https://nodejs.org/dist/v16.13.2/node-v16.13.2-darwin-arm64.tar.xz failed
grep: /Users/你的用户名/.nvm/.cache/bin/node-v16.13.2-darwin-arm64/node-v16.13.2-darwin-arm64.tar.xz: No such file or directory
Provided file to checksum does not exist.
Binary download failed, trying source.
Detected that you have 8 CPU core(s)
Running with 7 threads to speed up the build
Clang v3.5+ detected! CC or CXX not specified, will use Clang as C/C++ compiler!
mkdir: /Users/你的用户名/.nvm/.cache: Permission denied
creating directory /Users/你的用户名/.nvm/.cache/src/node-v16.13.2/files failed
Downloading https://nodejs.org/dist/v16.13.2/node-v16.13.2.tar.xz...
Warning: Failed to create the file                                             
Warning: /Users/你的用户名/.nvm/.cache/src/node-v16.13.2/node-v16.13.2.tar.xz: No 
Warning: such file or directory
                                                                            0.0%curl: (23) Failure writing output to destination

download from https://nodejs.org/dist/v16.13.2/node-v16.13.2.tar.xz failed
grep: /Users/你的用户名/.nvm/.cache/src/node-v16.13.2/node-v16.13.2.tar.xz: No such file or directory
Provided file to checksum does not exist.

3 查看全局变量,没有看到配置好的nvm配置

bash 复制代码
echo $PATH  

解决

结合以上三个问题都是因为安装nvm时,没有安装好!还有权限问题。

1 修复目录权限

bash 复制代码
sudo chown -R $(whoami) ~/.nvm

2 重新安装nvm

bash 复制代码
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

3 查看当前电脑的shell配置

bash 复制代码
echo $SHELL

可能输出是/bin/bash或者/bin/zsh

4 修改配置文件

打开配置文件

bash 复制代码
nano ~/.zshrc  # 或 nano ~/.bash_profile

在文件末尾添加

bash 复制代码
 export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # 加载 nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # 加载自动补全

5 重新启动配置文件

如果是bin/zsh,使用下面的命令:

bash 复制代码
source ~/.zshrc  

如果是/bin/bash,使用下面的命令:

bash 复制代码
 source ~/.bash_profile

6 验证nvm 和npm是否可以使用

bash 复制代码
nvm install #指定版本
nvm ls #看目前存在的版本
nvm --version  # 应显示 NVM 版本
node -v        # 应显示 Node.js 版本
npm -v         # 应显示 npm 版本
which node     # 应显示 ~/.nvm/versions/node/.../bin/node
which npm      # 应显示 ~/.nvm/versions/node/.../bin/npm

补充说明

打开/.zshrc 文件后,如何保存修改呢?

按 Ctrl + X后按Y确认保存,最后按Enter键保存。

相关推荐
愿你天黑有灯下雨有伞34 分钟前
枚举策略模式实战:优雅消除支付场景的if-else
java·开发语言·策略模式
楚轩努力变强40 分钟前
前端工程化常见问题总结
开发语言·前端·javascript·vue.js·visual studio code
鱼樱前端42 分钟前
rust基础二(闭包)
前端·rust
菜鸟学Python1 小时前
Python web框架王者 Django 5.0发布:20周年了!
前端·数据库·python·django·sqlite
Dcs1 小时前
gRPC性能陷阱:低延迟网络下的客户端瓶颈揭秘
java
前端开发爱好者1 小时前
只有 7 KB!前端圈疯传的 Vue3 转场动效神库!效果炸裂!
前端·javascript·vue.js
探索java1 小时前
深入解析 Spring 获取 XML 验证模式的过程
xml·java·spring
pe7er1 小时前
RESTful API 的规范性和接口安全性如何取舍
前端·后端
找不到、了1 小时前
Java设计模式之<装饰器模式>
java·设计模式·装饰器模式
Fly-ping1 小时前
【前端】JavaScript文件压缩指南
开发语言·前端·javascript