解决 pngquant failed to build, make sure that libpng is installed

作者: 科技微讯

日期:

我的网站 kejiweixun.com 是用 gatsbyjs 所写, 今天通过 npm update 升级 node packages 时, 终端提示以下错误:

> pngquant-bin@5.0.2 postinstall /Users/to/Documents/kejiweixun.com/node_modules/pngquant-bin
> node lib/install.js

  ⚠ read ECONNRESET
  ⚠ pngquant pre-build test failed
  ℹ compiling from source
  ✖ Error: pngquant failed to build, make sure that libpng is installed
    at /Users/to/Documents/kejiweixun.com/node_modules/bin-build/node_modules/execa/index.js:231:11
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Promise.all (index 0)

如果没记错这个问题我遇到不止一次, 但上一次应该是好几个月之前, 不记得当时怎么解决, 今天再遇到, 又花了大概半小时才找到解决办法, 而且很多人遇到了一样的问题, 所以在这里记录一下以防忘记, 当然这个方法不一定适用于每个人.

解决办法就是:

brew install pkg-config

在执行 brew install pkg-config 时我又遇到以下错误:

==> `brew cleanup` has not been run in 30 days, running now...
Error: Permission denied @ apply2files - /usr/local/lib/node_modules/gatsby-cli/node_modules/term-size/vendor/macos/.DS_Store

解决方法是:

sudo chown -R "$(whoami)":admin /usr/local/lib