M1 原生的 在 node15 没有发布 canvas 的二进制版本。因此安装 canvas时会自动从源码进行编译,但是如果没有安装相应依赖就会报错。
解决方案
arch -arm64 brew install pkg-config cairo pango jpeg giflib librsvg
故障排除
Homebrew 无法安装
如果遇到
Error: Failure while executing; `tar --extract --no-same-owner --file /Users/wenbing/Library/Caches/Homebrew/downloads/6f65cc908bca8f42eeb93f33e25d76d748a7a28f20f30f41befe472ea2a01bc5--fribidi-1.0.10.arm64_big_sur.bottle.tar.gz --directory /private/tmp/d20210618-36598-16scyl7` exited with 1. Here's the output:
tar: Error opening archive: Failed to open '/Users/wenbing/Library/Caches/Homebrew/downloads/6f65cc908bca8f42eeb93f33e25d76d748a7a28f20f30f41befe472ea2a01bc5--fribidi-1.0.10.arm64_big_sur.bottle.tar.gz'
这种报错,需要手动先安装依赖前置依赖,再安装模块
brew install libtiff gdk-pixbuf librsvg
node-gyp 找不到头文件
如果你的 homebrew 也是 m1 原生的,那么 homebrew 会安装在 /opt/homebrew 下。而 brew install 的东西都会在 /opt/homebrew/Cellar/下,不会软链接到 /usr/local/include 下。而 node-gyp 去编译的时候,本质上还是用 clang 去编译,找不到头文件,所以会报一个fatal error: 'jpeglib.h' file not found
。解决的办法就是把 /opt/homebrew/Cellar/加到 clang 的 include path 里。我的做法是在 ~/.bash_profile
中加入一行
export CPLUS_INCLUDE_PATH=/opt/homebrew/include