You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

macOS Catalina下含Git依赖的Node.js项目npm install执行失败求助

解决macOS Catalina下npm install因Git权限抛出EPERM错误的问题

最近在macOS Catalina上运行依赖Git的Node.js项目时,执行npm install遇到了烦人的EPERM错误——系统直接拒绝了操作,不过用sudo npm install就能正常执行,旧版macOS完全没这个问题。先把报错日志贴出来:

npm ERR! code EPERM
npm ERR! syscall spawn
npm ERR! errno EPERM
npm ERR! Error: spawn EPERM
npm ERR! at ChildProcess.spawn (internal/child_process.js:394:11)
npm ERR! at Object.spawn (child_process.js:540:9)
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/pacote/lib/util/git.js:216:24
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/promise-retry/index.js:29:24
npm ERR! OperationalError: spawn EPERM
npm ERR! at ChildProcess.spawn (internal/child_process.js:394:11)
npm ERR! at Object.spawn (child_process.js:540:9)
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/pacote/lib/util/git.js:216:24
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/promise-retry/index.js:29:24 {
npm ERR! cause: Error: spawn EPERM
npm ERR! at ChildProcess.spawn (internal/child_process.js:394:11)
npm ERR! at Object.spawn (child_process.js:540:9)
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/pacote/lib/util/git.js:216:24
npm ERR! at /Users/user/.node/lib/node_modules/npm/node_modules/promise-retry/index.js:29:24 {
npm ERR! errno: 'EPERM',
npm ERR! code: 'EPERM',
npm ERR! syscall: 'spawn'
npm ERR! },
npm ERR! isOperational: true,
npm ERR! stack: 'Error: spawn EPERM\n' +
npm ERR! ' at ChildProcess.spawn (internal/child_process.js:394:11)\n' +
npm ERR! ' at Object.spawn (child_process.js:540:9)\n' +
npm ERR! ' at /Users/user/.node/lib/node_modules/npm/node_modules/pacote/lib/util/git.js:216:24\n' +
npm ERR! ' at /Users/user/.node/lib/node_modules/npm/node_modules/promise-retry/index.js:29:24',
npm ERR! errno: 'EPERM',
npm ERR! code: 'EPERM',
npm ERR! syscall: 'spawn'
npm ERR! }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It is likely you do not have the permissions to access this file as the current user
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the npm ERR! permissions of the file and its containing directories, or try running npm ERR! the command again as root/Administrator.
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/user/.npm/_logs/2019-10-30T08_40_20_156Z-debug.log

这个问题本质是Catalina的安全管控比旧系统更严格,npm调用Git的时候没有足够的权限,下面是几个亲测有效的解决方法:

1. 修复npm全局目录的权限

全局npm的目录所有权可能被改成了root,导致普通用户无法操作。执行这条命令把权限改回当前用户:

sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}

执行完之后,再试试npm install,大概率能解决问题。

2. 检查Git的执行权限

有时候系统自带的Git可能权限有问题,先看看权限:

ls -la /usr/bin/git

正常的权限应该是-rwxr-xr-x,如果不是,就执行下面的命令添加执行权限:

sudo chmod +x /usr/bin/git

3. 切换到brew安装的Git(可选)

系统自带的Git可能受系统完整性保护(SIP)限制,用brew装的Git权限更宽松。先安装Git:

brew install git

然后配置npm使用这个Git:

npm config set git $(which git)

这样npm就会调用brew版本的Git,避开系统权限问题。

4. 别依赖sudo跑npm

虽然sudo npm install能临时解决,但长期使用会导致后续的模块安装、运行出现更多权限冲突(比如某个模块需要写入用户目录文件时又会报错),所以优先用前面的方法修复权限,别图省事一直用sudo。

内容的提问来源于stack exchange,提问作者Rasekaran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:30:28