Sharp error in mac os

/post/error-vips article cover image

在升级gatsby及其依赖时install模块时发现 ../src/common.cc:24:10: fatal error: 'vips/vips8' file not found,导致sharp模块不能正常安装。

经查资料,并非是sharp模块的原因而是libuvips模块未安装导致(应该和升级nodejs版本有关?):

报错信息

shell
make: Entering directory '/node_modules/sharp/build'
  TOUCH Release/obj.target/libvips-cpp.stamp
  CXX(target) Release/obj.target/sharp/src/common.o
../src/common.cc:25:22: fatal error: vips/vips8: No such file or directory
 #include <vips/vips8>
                      ^
compilation terminated.
sharp.target.mk:135: recipe for target 'Release/obj.target/sharp/src/common.o' failed
make: *** [Release/obj.target/sharp/src/common.o] Error 1
make: Leaving directory '/node_modules/sharp/build'

解决方法

  1. 前提确保node-gyp有正常安装
  2. 使用 <u>brew info vips</u> 查看其依赖安装情况,例如上面报错就是make模块调用报错
  3. 如果未安装过就直接 <u>brew install vips</u> ,或者某个模块未安装成功则可重新安装 <u>brew reinstall vips</u>
  4. 安装过程中,如果brew安装过慢可以通过设置VPN代理使用安装: USING HOMEBREW BEHIND A PROXY
  5. 安装完毕后,如果 npm i 还会有报错日志提示就清楚缓存重新安装
  6. npm cache clean -f
  7. npm install
shell
error /Users/.../node_modules/sharp: Command failed.
Exit code: 1
Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy)
Arguments:
Directory: /Users/.../node_modules/sharp
Output:
sharp: Downloading https://github.com/lovell/sharp-libvips/releases/download/v8.13.3/libvips-8.13.3

解决方法-1

  • xcode-select --install
  • brew install gcc
  • brew reinstall vips
  • brew info vips
  • yarn install

如果brew install vips一直不成功,可直接在 sharp libvips 中下载对应包解压至/Users/yourusername/.npm/_libvips

如果上面方法还不能解决:

  • brew remove vips
  • rm -rf node_modules
  • yarn install

使用阿里镜像

Sharp官方文档

shell
npm config set sharp_binary_host "https://npmmirror.com/mirrors/sharp"
npm config set sharp_libvips_binary_host "https://npmmirror.com/mirrors/sharp-libvips"
npm install sharp