You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node Canvas依赖安装报错:npm install时libffi相关问题求助

问题:npm install 时 canvas 依赖的 libffi 找不到报错

克隆了React+Vite的工作项目,执行npm install时遇到libffi相关错误,已尝试以下操作但问题仍未解决:

  • 通过Homebrew安装/重新安装libffi,并执行临时路径配置:
    export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig"
    
  • 添加--force参数执行npm install
  • 按照canvas官方文档执行brew依赖安装命令:
    brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman
    
  • 重新安装Homebrew包、降低防火墙、手动配置libffi路径

已安装的Homebrew包列表

cairo                   giflib                  jpeg                    libtiff                 libxrender              pkg-config
fontconfig              git                     jpeg-turbo              libx11                  lz4                     xorgproto
freetype                glib                    libffi                  libxau                  lzo                     xz
fribidi                 graphite2               libpng                  libxcb                  pango                   zlib
gdk-pixbuf              harfbuzz                libpthread-stubs        libxdmcp                pcre2                   zstd
gettext                 icu4c                   librsvg                 libxext                 pixman

npm 报错信息

npm WARN using --force Recommended protections disabled.
npm WARN deprecated @vitest/coverage-c8@0.24.5: v8 coverage is moved to @vitest/coverage-v8 package
npm WARN deprecated sourcemap-codec@1.4.8: Please use @jridgewell/sourcemap-codec instead
npm ERR! code 1
npm ERR! path /Users/hdhillon016/React/RelCheckWebUI/node_modules/canvas
npm ERR! command failed
npm ERR! command sh -c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.0.0
npm ERR! gyp info using node@16.15.0 | darwin | x64
npm ERR! gyp info find Python using Python version 3.10.8 found at "/usr/local/bin/python3"
npm ERR! gyp info spawn /usr/local/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'make',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/hdhillon016/React/RelCheckWebUI/node_modules/canvas/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/local/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/Users/hdhillon016/Library/Caches/node-gyp/16.15.0/include/node/common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=/Users/hdhillon016/Library/Caches/node-gyp/16.15.0',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/usr/local/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/Users/hdhillon016/Library/Caches/node-gyp/16.15.0/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/Users/hdhillon016/React/RelCheckWebUI/node_modules/canvas',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! Package libffi was not found in the pkg-config search path.
npm ERR! Perhaps you should add the directory containing `libffi.pc'
npm ERR! to the PKG_CONFIG_PATH environment variable
npm ERR! Package 'libffi', required by 'gobject-2.0', not found
npm ERR! gyp: Call to 'pkg-config pangocairo --cflags-only-I | sed s/-I//g' returned exit status 0 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:261:16)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:527:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
npm ERR! gyp ERR! System Darwin 22.6.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/hdhillon016/React/RelCheckWebUI/node_modules/canvas
npm ERR! gyp ERR! node -v v16.15.0
npm ERR! gyp ERR! node-gyp -v v9.0.0
npm ERR! gyp ERR! not ok

解决方案

1. 永久配置 PKG_CONFIG_PATH

临时配置的环境变量仅对当前终端会话有效,需写入shell配置文件永久生效:

  • Bash用户执行:
    echo 'export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"' >> ~/.bash_profile
    source ~/.bash_profile
    
  • Zsh用户执行:
    echo 'export PKG_CONFIG_PATH="/usr/local/opt/libffi/lib/pkgconfig:$PKG_CONFIG_PATH"' >> ~/.zshrc
    source ~/.zshrc
    

2. 重新链接libffi

修复Homebrew包的链接问题:

brew unlink libffi && brew link libffi --force

3. 清理缓存后重装依赖

清除npm和项目缓存,避免残留问题:

npm cache clean --force
rm -rf node_modules package-lock.json
npm install

4. 调整Python版本兼容性

部分旧版node-gyp对Python 3.x兼容性不佳,可临时指定Python 2.7:

npm config set python /usr/bin/python2.7
npm install
# 安装完成后可改回原Python版本
npm config set python /usr/local/bin/python3

5. 更新node-gyp到最新版

当前使用的node-gyp版本可能存在兼容问题,更新后重试:

npm install -g node-gyp@latest
npm install

内容的提问来源于stack exchange,提问作者Harry

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 14:35:03