执行node-gyp configure遇Error: spawn EPERM错误,求技术协助
问题背景
执行npm install sqlite3或appmetrics-dash等依赖安装命令时,持续触发Error: spawn EPERM错误,手动运行node-gyp configure也报相同错误,报错日志及npm配置如下:
node-gyp configure 报错日志
C:\Users\Jenny_Hung\test\d-api>node-gyp configure gyp info it worked if it ends with ok gyp info using node-gyp@9.3.1 gyp info using node@16.20.0 | win32 | x64 gyp info find Python using Python version 3.8.0 found at "C:\Users\Jenny_Hung\AppData\Local\Programs\Python\Python38\python.exe" gyp ERR! UNCAUGHT EXCEPTION gyp ERR! stack Error: spawn EPERM gyp ERR! stack at ChildProcess.spawn (node:internal/child_process:420:11) gyp ERR! stack at spawn (node:child_process:733:9) gyp ERR! stack at execFile (node:child_process:331:17) gyp ERR! stack at VisualStudioFinder.findVisualStudio2017OrNewer (C:\Users\Jenny_Hung\AppData\Roaming\nvm\v16.20.0\node_modules\node-gyp\lib\find-visualstudio.js:141:17) gyp ERR! stack at VisualStudioFinder.findVisualStudio (C:\Users\Jenny_Hung\AppData\Roaming\nvm\v16.20.0\node_modules\node-gyp\lib\find-visualstudio.js:63:10) gyp ERR! stack at findVisualStudio (C:\Users\Jenny_Hung\AppData\Roaming\nvm\v16.20.0\node_modules\node-gyp\lib\find-visualstudio.js:13:10) gyp ERR! stack at C:\Users\Jenny_Hung\AppData\Roaming\nvm\v16.20.0\node_modules\node-gyp\lib\configure.js:87:9) gyp ERR! stack at FSReqCallback.oncomplete (node:fs:192:23) gyp ERR! System Windows_NT 10.0.19045 gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" gyp ERR! cwd C:\Users\Jenny_Hung\test\d-api gyp ERR! node -v v16.20.0 gyp ERR! node-gyp -v v9.3.1 gyp ERR! Node-gyp failed to build your package. gyp ERR! Try to update npm and/or node-gyp and if it does not help file an issue with the package author.
npm 配置信息
C:\Users\Jenny_Hung\test\d-api>npm config list ; "global" config from C:\Program Files\nodejs\etc\npmrc ; msvs_version = "2017" ; overridden by user ; "user" config from C:\Users\Jenny_Hung\.npmrc msbuild_path = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\MSBuild\\15.0\\Bin\\MSBuild.exe" msvs_version = "2017" python = "C:\\Users\\Jenny_Hung\\AppData\\Local\\Programs\\Python\\Python38\\python.exe" ; node bin location = C:\Program Files\nodejs\node.exe ; node version = v16.20.0 ; npm local prefix = C:\Users\Jenny_Hung\test\d-api ; npm version = 8.19.4 ; cwd = C:\Users\Jenny_Hung\test\d-api ; HOME = C:\Users\Jenny_Hung ; Run `npm config ls -l` to show all defaults.
解决步骤
以管理员身份运行终端
Windows下spawn EPERM错误大多和权限不足有关,右键命令提示符/PowerShell,选择「以管理员身份运行」,再执行node-gyp configure和npm install。检查Visual Studio 2017组件完整性
打开Visual Studio Installer,确认已安装「使用C++的桌面开发」工作负载,该组件包含node-gyp编译原生模块必需的MSVC工具链、Windows SDK等;可选安装「Node.js开发」组件简化配置。验证MSBuild路径与权限
手动打开配置的MSBuild路径C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe,确认能正常启动;右键该文件→属性→安全,确保当前用户拥有「读取和执行」权限。清理缓存与重新安装
- 执行
node-gyp clean清理node-gyp缓存 - 删除项目目录下的
node_modules文件夹和package-lock.json文件 - 重新运行
npm install
- 执行
调整node-gyp版本
Node.js 16.x与node-gyp 8.x版本兼容性更稳定,执行npm install -g node-gyp@8.4.1降级全局node-gyp,再重试命令。配置MSBuild环境变量
将MSBuild所在路径C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin添加到系统环境变量PATH中,重启终端后生效,再执行安装命令。
内容的提问来源于stack exchange,提问作者chen

