今天需要搞一个服务端渲染 ECharts 的图标的方案。

于是根据 官方网站 上的推荐找到这个项目:https://github.com/hellosean1025/node-echarts

根据文档,本地(macOS)测试时,需要安装以下依赖:

brew install pkg-config cairo pango libpng jpeg giflib

但安装完,npm install 依赖后,在执行官方的 demo 时报错:

Error: Cannot find module '../build/Release/canvas'

根据提示,找了一圈,说是要 canvas 没有安装正确,于是重新 npm i canvas ,结果报错

npm ERR! Package harfbuzz was not found in the pkg-config search path.
npm ERR! Perhaps you should add the directory containing `harfbuzz.pc'
npm ERR! to the PKG_CONFIG_PATH environment variable
npm ERR! Package 'harfbuzz', required by 'pango', not found
npm ERR! gyp: Call to 'pkg-config pangocairo --libs' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
npm ERR! gyp ERR! configure error 
npm ERR! gyp ERR! stack Error: `gyp` failed with exit code: 1
npm ERR! gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:375:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
npm ERR! gyp ERR! System Darwin 20.3.0
npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd /Users/tony/demo/echarts-ssr/node_modules/canvas
npm ERR! gyp ERR! node -v v14.17.3
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok

其中最关键的一句是:

Package 'harfbuzz', required by 'pango', not found

于是果断执行 brew install harfbuzz,但不 work。

找了一圈,找到一个 react 项目相关的同样报错

参考文中的解决方案,除了 install 之外,还要 link,只能怪自己不小心没有看安装完成的提示信息了:

brew install harfbuzz
brew link harfbuzz

如果 brew link harfbuzzz 报错:

Linking /usr/local/Cellar/harfbuzz/2.8.2...
Error: Could not symlink lib/cmake/harfbuzz
/usr/local/lib/cmake is not writable.

则说明没有权限,需要执行:

sudo chown -R $(whoami) /usr/local/lib/cmake

上面执行完后再执行: brew link harfbuzz 成功。

问题解决。

标签: nodejs, canvas, npm, echarts