Mac M1执行npm install遇权限拒绝及目录非空问题求助
解决Mac M1上npm install的EACCES权限错误与ENOTEMPTY目录非空错误
问题重现
执行npm install时先后触发两类错误:
- EACCES权限拒绝:无法创建npm缓存目录,提示权限不足且目标目录已存在
- ENOTEMPTY目录非空:重命名
@vue/compiler-core目录失败,目标临时目录非空
错误日志详情:
a@Air-von-A kanban % npm install npm ERR! code EEXIST npm ERR! syscall mkdir npm ERR! path /Users/a/.npm/_cacache/content-v2/sha512/0f/7d npm ERR! errno -13 npm ERR! EACCES: permission denied, mkdir '/Users/a/.npm/_cacache/content-v2/sha512/0f/7d' npm ERR! File exists: /Users/a/.npm/_cacache/content-v2/sha512/0f/7d npm ERR! Remove the existing file and try again, or run npm npm ERR! with --force to overwrite files recklessly. npm ERR! A complete log of this run can be found in: /Users/a/.npm/_logs/2024-01-06T17_01_26_417Z-debug-0.log a@Air-von-A kanban % sudo npm install -g yarn added 1 package in 650ms a@Air-von-A kanban % npm install npm ERR! code ENOTEMPTY npm ERR! syscall rename npm ERR! path /Users/a/Downloads/kanban/node_modules/@vue/compiler-core npm ERR! dest /Users/a/Downloads/kanban/node_modules/@vue/.compiler-core-ZXEc73Pt npm ERR! errno -66 npm ERR! ENOTEMPTY: directory not empty, rename '/Users/a/Downloads/kanban/node_modules/@vue/compiler-core' -> '/Users/a/Downloads/kanban/node_modules/@vue/.compiler-core-ZXEc73Pt' npm ERR! A complete log of this run can be found in: /Users/avissataghizadeh/.npm/_logs/2024-01-06T17_02_33_402Z-debug-0.log
解决方案
1. 修复npm缓存目录权限
大概率是之前用sudo执行过npm命令,导致个人目录下的.npm缓存目录所有权归了root,当前用户无操作权限。执行命令把权限改回当前用户:
sudo chown -R $USER:$GROUP ~/.npm
2. 强制清理损坏的npm缓存
缓存目录已出现文件冲突,直接强制清理:
npm cache clean --force
3. 清理项目残留依赖文件
项目中的node_modules已损坏,配套的锁文件也可能存在异常,一并删除:
rm -rf node_modules package-lock.json
4. 重新安装依赖
回到项目根目录,重新执行安装命令:
npm install
可选:用nvm管理Node版本(从根源避免权限问题)
如果后续频繁遇到权限问题,推荐用nvm管理Node.js版本——它会将Node及相关依赖安装在用户目录下,无需触碰系统级权限:
- 通过官方脚本安装nvm(安装完成后重启终端)
- 安装并使用长期支持版Node:
nvm install --lts nvm use --lts
内容的提问来源于stack exchange,提问作者Avissa Taghizadeh
相关产品推荐
相关产品推荐

