我在配备 M1 CPU 的 Mac 上遇到以下错误:
I'm unable to build nodejs app with 'node-canvas' npm package, because Error: dlopen(/Users/..../node_modules/canvas/build/Release/canvas.node, 0x0001): tried: '/Users/..../node_modules/canvas/build/Release/canvas.node' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/System/Volumes/Preboot/Cryptexes/OS/..../node_modules/canvas/build/Release/canvas.node' (no such file), '/Users/..../node_modules/canvas/build/Release/canvas.node' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64'))
我安装了brew canvas依赖项:
brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman
我还设置了以下路径:
export PKG_CONFIG_PATH="/opt/homebrew/opt/pixman/lib/pkgconfig"
我还尝试从源代码重建:
npm install --build-from-source
我还删除并再次安装了画布包:
rm -rf node_modules package-lock.json npm install
但我仍然遇到与
arm64
架构相关的相同错误。
通知:
nodejs is installed with nvm
nodejs version: v22.11.0
nvm version: 0.39.7
感谢您的帮助。
要修复 M1 Mac 上的节点画布构建问题:
为 ARM64 重新安装 Node.js:
nvm uninstall v22.11.0
arch -arm64 nvm install v22.11.0
安装 ARM64 的依赖项:
brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman
export PKG_CONFIG_PATH="/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/pixman/lib/pkgconfig"
清理重新安装画布:
rm -rf node_modules package-lock.json
npm install
npm rebuild canvas --build-from-source
为了结果
node -e "require('canvas')"