MacOS Ventura下Gatsby项目npm安装sharp libvips报错求助
Gatsby项目npm install时sharp编译失败的问题解决
环境与问题详情
- 系统:MacOS Ventura
- 声明Node版本:18.9(实际运行版本为19.4.0,存在版本冲突)
- 项目依赖:
gatsby-plugin-sharp: ^2.3.5gatsby-transformer-sharp: ^2.3.7- 指定sharp版本:0.31.1
- 执行命令:
sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy) - 已尝试操作:删除tar.br缓存文件、通过HomeBrew安装vips、npm/yarn安装sharp(npm失败,yarn调试后可运行)
- 核心错误日志:
npm ERR! code 1 npm ERR! path /Users/seanshe/Desktop/bdab_website/node_modules/gatsby-transformer-sharp/node_modules/sharp npm ERR! command failed npm ERR! command sh -c (node install/libvips && node install/dll-copy && prebuild-install) || (node-gyp rebuild && node install/dll-copy) npm ERR! CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o npm ERR! LIBTOOL-STATIC Release/nothing.a npm ERR! TOUCH Release/obj.target/libvips-cpp.stamp npm ERR! CXX(target) Release/obj.target/sharp/src/common.o npm ERR! info sharp Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.10.5/libvips-8.10.5-darwin-arm64v8.tar.br npm ERR! ERR! sharp Prebuilt libvips 8.10.5 binaries are not yet available for darwin-arm64v8 npm ERR! info sharp Attempting to build from source via node-gyp but this may fail due to the above error npm ERR! info sharp Please see https://sharp.pixelplumbing.com/install for required dependencies npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.3.0 npm ERR! gyp info using node@19.4.0 | darwin | arm64 npm ERR! gyp info find Python using Python version 3.10.9 found at "/opt/homebrew/opt/python@3.10/bin/python3.10" npm ERR! gyp info spawn /opt/homebrew/opt/python@3.10/bin/python3.10 npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args '/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'make', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/seanshe/Desktop/bdab_website/node_modules/gatsby-transformer-sharp/node_modules/sharp/build/config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args '/Users/seanshe/Library/Caches/node-gyp/19.4.0/include/node/common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=/Users/seanshe/Library/Caches/node-gyp/19.4.0', npm ERR! gyp info spawn args '-Dnode_gyp_dir=/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=/Users/seanshe/Library/Caches/node-gyp/19.4.0/<(target_arch)/node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=/Users/seanshe/Desktop/bdab_website/node_modules/gatsby-transformer-sharp/node_modules/sharp', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp info spawn make npm ERR! gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ] npm ERR! warning: /Library/Developer/CommandLineTools/usr/bin/libtool: archive library: Release/nothing.a the table of contents is empty (no object file members in the library define global symbols) npm ERR! ../src/common.cc:24:10: fatal error: 'vips/vips8' file not found npm ERR! #include <vips/vips8> npm ERR! ^~~~~~~~~~~~ npm ERR! 1 error generated. npm ERR! make: *** [Release/obj.target/sharp/src/common.o] Error 1 npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2 npm ERR! gyp ERR! stack at ChildProcess.onExit (/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:203:23) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:513:28) npm ERR! gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:293:12) npm ERR! gyp ERR! System Darwin 22.2.0 npm ERR! gyp ERR! command "/opt/homebrew/Cellar/node/19.4.0/bin/node" "/opt/homebrew/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild" npm ERR! gyp ERR! cwd /Users/seanshe/Desktop/bdab_website/node_modules/gatsby-transformer-sharp/node_modules/sharp npm ERR! gyp ERR! node -v v19.4.0 npm ERR! gyp ERR! node-gyp -v v9.3.0 npm ERR! gyp ERR! not ok npm ERR! A complete log of this run can be found in: npm ERR! /Users/seanshe/.npm/_logs/2023-01-11T05_40_20_443Z-debug-0.log
解决步骤
1. 修复Node版本冲突
日志显示实际运行Node为19.4.0,与声明的18.9不一致,先统一版本:
- 用nvm切换到指定版本:
nvm use 18.9 - 清除npm缓存:
npm cache clean --force
2. 升级Gatsby sharp依赖(推荐)
当前gatsby-plugin-sharp和gatsby-transformer-sharp为v2.x,版本过旧,不支持arm64架构和新版本sharp:
- 修改package.json中的依赖:
"gatsby-plugin-sharp": "^4.25.0", "gatsby-transformer-sharp": "^4.25.0" - 删除旧依赖文件:
rm -rf node_modules package-lock.json - 重新安装:
npm install
3. 手动指定libvips路径(若升级后仍报错)
确保HomeBrew安装的vips被node-gyp识别:
- 重新安装vips:
brew reinstall vips - 设置环境变量:
export PKG_CONFIG_PATH="/opt/homebrew/opt/vips/lib/pkgconfig" export CPPFLAGS="-I/opt/homebrew/opt/vips/include" export LDFLAGS="-L/opt/homebrew/opt/vips/lib" - 再次执行
npm install
4. 降级sharp适配老Gatsby(不推荐,仅保留老版本时用)
若必须使用Gatsby v2.x,需降级sharp到兼容libvips 8.10.5的版本:
- 在package.json中添加:
"sharp": "^0.29.3" - 执行
npm install强制使用该版本
内容的提问来源于stack exchange,提问作者Rebecca
相关产品推荐
相关产品推荐

