code EACCES npm error syscall mkdir npm error path /usr/local/lib/node_modules/@...
🚨 错误信息
npm install -g @anthropic-ai/claude-code
npm error code EACCES
npm error syscall mkdir
npm error path /usr/local/lib/node_modules/@anthropic-ai
npm error errno -13
npm error Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@anthropic-ai'
npm error at async mkdir (node:internal/fs/promises:861:10)
npm error at async /usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:576:20
npm error at async Promise.allSettled (index 0)
npm error at async [reifyPackages] (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:313:11)
npm error at async Arborist.reify (/usr/local/lib/node_modules/npm/node_modules/@npmcli/arborist/lib/arborist/reify.js:125:5)
npm error at async Install.exec (/usr/local/lib/node_modules/npm/lib/commands/install.js:150:5)
npm error at async Npm.exec (/usr/local/lib/node_modules/npm/lib/npm.js:208:9)
npm error at async module.exports (/usr/local/lib/node_modules/npm/lib/cli/entry.js:67:5) {
npm error errno: -13,
npm error code: 'EACCES',
npm error syscall: 'mkdir',
npm error path: '/usr/local/lib/node_modules/@anthropic-ai'
npm error }
npm error
npm error The operation was rejected by your operating system.
npm error It is likely you do not have the permissions to access this file as the current user
npm error
npm error If you believe this might be a permissions issue, please double-check the
npm error permissions of the file and its containing directories, or try running
npm error the command again as root/Administrator.
npm error A complete log of this run can be found in: /Users/hans/.npm/_logs/2026-01-03T05_33_54_804Z-debug-0.log
🤖 AI解决方案
错误翻译
原文:Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules/@anthropic-ai'
译文:错误:EACCES:权限被拒绝,无法创建目录 '/usr/local/lib/node_modules/@anthropic-ai'
原因分析
根本原因:您没有足够的权限向 /usr/local/lib/node_modules/ 目录写入文件。
具体分析:当您使用 sudo 权限运行 npm install -g 命令时,npm 试图在全局安装目录 /usr/local/lib/node_modules/ 中创建新的包目录。但当前用户没有该目录的写权限,导致 mkdir 操作被系统拒绝(错误码 EACCES)。
解决方案
方案一:使用管理员权限(推荐)
bash
sudo npm install -g @anthropic-ai/claude-code
方案二:修复 npm 目录权限(长期方案)
bash
1. 更改 npm 全局安装目录的所有权到当前用户
sudo chown -R $USER /usr/local/lib/node_modules2. 更改 npm 缓存目录的所有权
sudo chown -R $USER ~/.npm3. 然后再次安装(无需 sudo)
npm install -g @anthropic-ai/claude-code
方案三:使用 Node Version Manager (NVM)
如果您使用 NVM 管理 Node.js 版本,npm 会将包安装到用户目录,完全避免权限问题:
bash
首先确保已安装 NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash然后重新打开终端,安装最新版 Node.js
nvm install --lts
nvm use --lts现在可以无权限问题安装
npm install -g @anthropic-ai/claude-code
️ 预防措施
最佳实践
sudo npm install -g,这会为系统安全带来风险。优先采用方案二修复权限或使用 NVM。工具推荐
ls -la /usr/local/lib/node_modules 查看目录权限状态