React-Native执行npm i触发node-gyp错误求助
解决方案:React Native项目npm install时sharp依赖报错(prebuild-install未识别+Windows SDK缺失)
问题分析
你的package.json里没直接引入sharp,但它是项目某个依赖包的间接依赖。报错分为两部分:
prebuild-install命令未被识别:这是sharp预构建脚本依赖的工具- node-gyp无法找到可用VS环境:已安装BuildTools但缺少Windows SDK,导致编译sharp失败
解决步骤
1. 安装prebuild-install工具
全局安装(推荐):
npm install -g prebuild-install
或者本地安装到项目:
npm install prebuild-install --save-dev
2. 补装Windows SDK
打开Visual Studio Installer,选择你已安装的VS 2022/2019 BuildTools实例,点击「修改」:
- 在「工作负载」面板,确认勾选「桌面开发使用C++」
- 切换到「单个组件」面板,找到Windows 10 SDK(版本选择与你系统匹配的,你的系统是Windows 10 19045,选10.0.19041.x系列即可),勾选后点击「修改」完成安装
- 安装完成后重启电脑
3. 清理缓存重新安装
# Windows系统删除node_modules用:rd /s /q node_modules rm -rf node_modules # 删除package-lock.json del package-lock.json # 清理npm缓存 npm cache clean --force # 重新安装依赖 npm install
4. 可选:指定node-gyp使用的VS版本
如果还是识别不到VS,手动设置版本:
# 对应VS2022 npm config set msvs_version 2022 # 对应VS2019则用:npm config set msvs_version 2019
完整错误日志
npm ERR! code 1 npm ERR! path E:\snk\node_modules\sharp npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy) npm ERR! sharp: Using cached C:\Users\Michal\AppData\Local\npm-cache\_libvips\libvips-8.14.5-win32-x64.tar.br npm ERR! sharp: Integrity check passed for win32-x64 npm ERR! sharp: Creating E:\snk\node_modules\sharp\build\Release npm ERR! sharp: Copying DLLs from E:\snk\node_modules\sharp\vendor\8.14.5\win32-x64\lib to E:\snk\node_modules\sharp\build\Release npm ERR! 'prebuild-install' is not recognized as an internal or external command, npm ERR! operable program or batch file. npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@10.0.1 npm ERR! gyp info using node@21.7.3 | win32 | x64 npm ERR! gyp info find Python using Python version 3.12.3 found at "C:\Python312\python.exe" npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt npm ERR! gyp ERR! find VS checking VS2022 (17.9.34728.123) found at: npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools" npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features" npm ERR! gyp ERR! find VS - found VC++ toolset: v143 npm ERR! gyp ERR! find VS - missing any Windows SDK npm ERR! gyp ERR! find VS checking VS2019 (16.11.34729.46) found at: npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools" npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features" npm ERR! gyp ERR! find VS - found VC++ toolset: v142 npm ERR! gyp ERR! find VS - missing any Windows SDK npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use npm ERR! gyp ERR! find VS not looking for VS2015 as it is only supported up to Node.js 18 npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8 npm ERR! gyp ERR! find VS npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload. npm ERR! gyp ERR! find VS For more information consult the documentation at: npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows npm ERR! gyp ERR! find VS ************************************************************** npm ERR! gyp ERR! find VS npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use npm ERR! gyp ERR! stack at VisualStudioFinder.fail (C:\Users\Michal\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:113:11) npm ERR! gyp ERR! stack at VisualStudioFinder.findVisualStudio (C:\Users\Michal\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:69:17) npm ERR! gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:95:5) npm ERR! gyp ERR! stack at async createBuildDir (C:\Users\Michal\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\configure.js:69:26) npm ERR! gyp ERR! stack at async run (C:\Users\Michal\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js:81:18) npm ERR! gyp ERR! System Windows_NT 10.0.19045 npm ERR! gyp ERR! command "D:\\Program Files\\Nodejs\\node.exe" "C:\\Users\\Michal\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd E:\snk\node_modules\sharp npm ERR! gyp ERR! node -v v21.7.3 npm ERR! gyp ERR! node-gyp -v v10.0.1 npm ERR! gyp ERR! not ok npm ERR! A complete log of this run can be found in: C:\Users\Michal\AppData\Local\npm-cache\_logs\2024-04-12T01_47_32_159Z-debug-0.log
内容的提问来源于stack exchange,提问作者Michał Frączek
相关产品推荐
相关产品推荐

