开发者问题收集

无法在 M1 Mac 上安装 node-canvas

2022-10-28
8027

需要帮助在 M1 Mac 上安装 node-canvas。这是日志。

yarn install

...
➤ YN0000: ┌ Link step
➤ YN0007: │ canvas@npm:2.10.1 must be built because it never has been before or the last one failed
➤ YN0009: │ canvas@npm:2.10.1 couldn't be built successfully (exit code 1, logs can be found here: /private/tmp/xfs-97e0a91f/build.log)
➤ YN0000: └ Completed in 14s 140ms
➤ YN0000: Failed with errors in 14s 708ms
# This file contains the result of Yarn building a package (canvas@npm:2.10.1)
# Script name: install

node-pre-gyp info it worked if it ends with ok
node-pre-gyp info using [email protected]
node-pre-gyp info using [email protected] | darwin | arm64
node-pre-gyp http GET https://github.com/Automattic/node-canvas/releases/download/v2.10.1/canvas-v2.10.1-node-v108-darwin-unknown-arm64.tar.gz
node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.10.1/canvas-v2.10.1-node-v108-darwin-unknown-arm64.tar.gz 
node-pre-gyp WARN Pre-built binaries not installable for [email protected] and [email protected] (node-v108 ABI, unknown) (falling back to source compile with node-gyp) 
node-pre-gyp WARN Hit error response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.10.1/canvas-v2.10.1-node-v108-darwin-unknown-arm64.tar.gz 
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | arm64
gyp info ok 
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | arm64
gyp info find Python using Python version 3.10.8 found at "/opt/homebrew/opt/[email protected]/bin/python3.10"
gyp info spawn /opt/homebrew/opt/[email protected]/bin/python3.10
gyp info spawn args [
gyp info spawn args   '/Users/m1macuser/Code/projectfolder/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'make',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/m1macuser/Code/projectfolder/node_modules/node-gyp/addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   '/Users/m1macuser/Library/Caches/node-gyp/18.9.1/include/node/common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=/Users/m1macuser/Library/Caches/node-gyp/18.9.1',
gyp info spawn args   '-Dnode_gyp_dir=/Users/m1macuser/Code/projectfolder/node_modules/node-gyp',
gyp info spawn args   '-Dnode_lib_file=/Users/m1macuser/Library/Caches/node-gyp/18.9.1/<(target_arch)/node.lib',
gyp info spawn args   '-Dmodule_root_dir=/Users/m1macuser/Code/projectfolder/node_modules/canvas',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'build',
gyp info spawn args   '-Goutput_dir=.'
gyp info spawn args ]
gyp info ok 
gyp info it worked if it ends with ok
gyp info using [email protected]
gyp info using [email protected] | darwin | arm64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory '/Users/m1macuser/Code/projectfolder/node_modules/canvas/build'
  SOLINK_MODULE(target) Release/canvas-postbuild.node
  CXX(target) Release/obj.target/canvas/src/backend/Backend.o
In file included from ../src/backend/Backend.cc:1:
../src/backend/Backend.h:60:14: warning: private field 'backend' is not used [-Wunused-private-field]
    Backend* backend;
             ^
1 warning generated.
  CXX(target) Release/obj.target/canvas/src/backend/ImageBackend.o
  CXX(target) Release/obj.target/canvas/src/backend/PdfBackend.o
  CXX(target) Release/obj.target/canvas/src/backend/SvgBackend.o
  CXX(target) Release/obj.target/canvas/src/bmp/BMPParser.o
  CXX(target) Release/obj.target/canvas/src/Backends.o
  CXX(target) Release/obj.target/canvas/src/Canvas.o
In file included from ../src/Canvas.cc:21:
../src/Util.h:18:7: warning: 'SetAccessor' is deprecated [-Wdeprecated-declarations]
        Nan::SetAccessor(
             ^
../../nan/nan.h:2512:1: note: 'SetAccessor' has been explicitly marked deprecated here
NAN_DEPRECATED inline void SetAccessor(
^
../../nan/nan.h:110:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
1 warning generated.
  CXX(target) Release/obj.target/canvas/src/CanvasGradient.o
  CXX(target) Release/obj.target/canvas/src/CanvasPattern.o
  CXX(target) Release/obj.target/canvas/src/CanvasRenderingContext2d.o
In file included from ../src/CanvasRenderingContext2d.cc:19:
../src/Util.h:18:7: warning: 'SetAccessor' is deprecated [-Wdeprecated-declarations]
        Nan::SetAccessor(
             ^
../../nan/nan.h:2512:1: note: 'SetAccessor' has been explicitly marked deprecated here
NAN_DEPRECATED inline void SetAccessor(
^
../../nan/nan.h:110:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
1 warning generated.
  CXX(target) Release/obj.target/canvas/src/closure.o
  CXX(target) Release/obj.target/canvas/src/color.o
  CXX(target) Release/obj.target/canvas/src/Image.o
In file included from ../src/Image.cc:11:
../src/Util.h:18:7: warning: 'SetAccessor' is deprecated [-Wdeprecated-declarations]
        Nan::SetAccessor(
             ^
../../nan/nan.h:2512:1: note: 'SetAccessor' has been explicitly marked deprecated here
NAN_DEPRECATED inline void SetAccessor(
^
../../nan/nan.h:110:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
../src/Image.cc:1164:3: warning: 'rsvg_handle_get_dimensions' is deprecated: Use 'rsvg_handle_get_intrinsic_size_in_pixels' instead [-Wdeprecated-declarations]
  rsvg_handle_get_dimensions(_rsvg, dims);
  ^
/nix/store/5b64vfc6bhip3xwqg3nvb8bv80wn5rf4-librsvg-2.55.1-dev/include/librsvg-2.0/librsvg/rsvg.h:705:1: note: 'rsvg_handle_get_dimensions' has been explicitly marked deprecated here
RSVG_DEPRECATED_FOR(rsvg_handle_get_intrinsic_size_in_pixels)
^
/nix/store/5b64vfc6bhip3xwqg3nvb8bv80wn5rf4-librsvg-2.55.1-dev/include/librsvg-2.0/librsvg/rsvg.h:50:32: note: expanded from macro 'RSVG_DEPRECATED_FOR'
#define RSVG_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) RSVG_API
                               ^
/nix/store/dp31bsk70zdyh46jpxzjwasbqlv2ncgv-glib-2.74.0-dev/include/glib-2.0/glib/gmacros.h:1211:44: note: expanded from macro 'G_DEPRECATED_FOR'
#define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
                                           ^
../src/Image.cc:1203:24: warning: 'rsvg_handle_render_cairo' is deprecated: Use 'rsvg_handle_render_document' instead [-Wdeprecated-declarations]
  gboolean render_ok = rsvg_handle_render_cairo(_rsvg, cr);
                       ^
/nix/store/5b64vfc6bhip3xwqg3nvb8bv80wn5rf4-librsvg-2.55.1-dev/include/librsvg-2.0/librsvg/rsvg-cairo.h:87:1: note: 'rsvg_handle_render_cairo' has been explicitly marked deprecated here
RSVG_DEPRECATED_FOR(rsvg_handle_render_document)
^
/nix/store/5b64vfc6bhip3xwqg3nvb8bv80wn5rf4-librsvg-2.55.1-dev/include/librsvg-2.0/librsvg/rsvg.h:50:32: note: expanded from macro 'RSVG_DEPRECATED_FOR'
#define RSVG_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f) RSVG_API
                               ^
/nix/store/dp31bsk70zdyh46jpxzjwasbqlv2ncgv-glib-2.74.0-dev/include/glib-2.0/glib/gmacros.h:1211:44: note: expanded from macro 'G_DEPRECATED_FOR'
#define G_DEPRECATED_FOR(f) __attribute__((__deprecated__("Use '" #f "' instead")))
                                           ^
3 warnings generated.
  CXX(target) Release/obj.target/canvas/src/ImageData.o
In file included from ../src/ImageData.cc:5:
../src/Util.h:18:7: warning: 'SetAccessor' is deprecated [-Wdeprecated-declarations]
        Nan::SetAccessor(
             ^
../../nan/nan.h:2512:1: note: 'SetAccessor' has been explicitly marked deprecated here
NAN_DEPRECATED inline void SetAccessor(
^
../../nan/nan.h:110:40: note: expanded from macro 'NAN_DEPRECATED'
# define NAN_DEPRECATED __attribute__((deprecated))
                                       ^
1 warning generated.
  CXX(target) Release/obj.target/canvas/src/init.o
  CXX(target) Release/obj.target/canvas/src/register_font.o
../src/register_font.cc:9:10: fatal error: 'CoreText/CoreText.h' file not found
#include <CoreText/CoreText.h>
         ^~~~~~~~~~~~~~~~~~~~~
1 error generated.
make: *** [canvas.target.mk:176: Release/obj.target/canvas/src/register_font.o] Error 1
make: Leaving directory '/Users/m1macuser/Code/projectfolder/node_modules/canvas/build'
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/Users/m1macuser/Code/projectfolder/node_modules/node-gyp/lib/build.js:203:23)
gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:291:12)
gyp ERR! System Darwin 21.6.0
gyp ERR! command "/nix/store/5y4bd2r99zhdbir95w5pf51bwfg37bwa-nodejs-18.9.1/bin/node" "/Users/m1macuser/Code/projectfolder/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--update-binary" "--module=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v108"
gyp ERR! cwd /Users/m1macuser/Code/projectfolder/node_modules/canvas
gyp ERR! node -v v18.9.1
gyp ERR! node-gyp -v v9.3.0
gyp ERR! not ok 
node-pre-gyp ERR! build error 
node-pre-gyp ERR! stack Error: Failed to execute '/nix/store/5y4bd2r99zhdbir95w5pf51bwfg37bwa-nodejs-18.9.1/bin/node /Users/m1macuser/Code/projectfolder/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --update-binary --module=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/Users/m1macuser/Code/projectfolder/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
node-pre-gyp ERR! stack     at ChildProcess._handle.onexit (node:internal/child_process:302:5)
node-pre-gyp ERR! System Darwin 21.6.0
node-pre-gyp ERR! command "/nix/store/5y4bd2r99zhdbir95w5pf51bwfg37bwa-nodejs-18.9.1/bin/node" "/Users/m1macuser/Code/projectfolder/node_modules/@mapbox/node-pre-gyp/bin/node-pre-gyp" "install" "--fallback-to-build" "--update-binary"
node-pre-gyp ERR! cwd /Users/m1macuser/Code/projectfolder/node_modules/canvas
node-pre-gyp ERR! node -v v18.9.1
node-pre-gyp ERR! node-pre-gyp -v v1.0.10
node-pre-gyp ERR! not ok 
Failed to execute '/nix/store/5y4bd2r99zhdbir95w5pf51bwfg37bwa-nodejs-18.9.1/bin/node /Users/m1macuser/Code/projectfolder/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --update-binary --module=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/m1macuser/Code/projectfolder/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v108' (1)

这似乎会引发错误:

1 warning generated.
  CXX(target) Release/obj.target/canvas/src/init.o
  CXX(target) Release/obj.target/canvas/src/register_font.o
../src/register_font.cc:9:10: fatal error: 'CoreText/CoreText.h' file not found
#include <CoreText/CoreText.h>

我被卡住了。请帮忙!

其他说明:

  • OSX 12.6
  • Node v.18(使用 nix 安装)
  • 已安装 Xcode 和 Xcode-select
  • Nix 用于安装其他 lib deps(如您在日志中看到的那样)

Node 构建:

file node_modules/canvas/build/Release/canvas-postbuild.node 
node_modules/canvas/build/Release/canvas-postbuild.node: Mach-O 64-bit arm64 bundle, flags:<NOUNDEFS|DYLDLINK|TWOLEVEL>

Nix shell:

# { pkgs ? import <nixpkgs> {} }:
with import <nixpkgs> {};

stdenv.mkDerivation {
  name = "myproject";

  nativeBuildInputs = [ pkg-config ];
  
  buildInputs = [
    nodejs-18_x
    yarn
    jdk
    docker
    chromedriver
    docker
    rustup
    libuuid
    act
    cairo
    pango
    pkg-config
    nodePackages.node-gyp
    libpng
    llvm
    librsvg
    pixman
    giflib
    libjpeg
  ];

    
  # APPEND_LIBRARY_PATH = "${lib.makeLibraryPath [ libGL libuuid google-chrome-dev]}";

  # shellHook = ''
  #   LD=$CC
  #   export LD_LIBRARY_PATH="$APPEND_LIBRARY_PATH:$LD_LIBRARY_PATH"
  # '';

}
2个回答

这篇文章让我找到了一个解决方案: 如何在 Nix 环境中使 clang 可以使用 macOS 框架?

通过将 pkgs.darwin.apple_sdk.frameworks.CoreText 添加到我的 buildInputs 并将 export NIX_LDFLAGS="-F${pkgs.darwin.apple_sdk.frameworks.CoreText}/Library/Frameworks -framework CoreText $NIX_LDFLAGS"; 添加到我的 shellHook,我能够将独立副本复制到 /nix/store ,与主机系统分开。这就是它最初未找到的原因

a11hard
2022-10-28

在 m1 mac 中安装 canvas 之前,使用此命令安装其他依赖项

brew install pkg-config cairo pango libpng jpeg giflib librsvg
Sougata Das
2023-07-22