windows 环境安装 Node.js 依赖 canvas 出错的解决方案

npm install 出错:


npm error path c:\code\trae\nodejs\snow\node_modules\canvas
npm error command failed
npm error command C:\app\Git20180223\Git20180223\bin\bash.exe -c node-pre-gyp install --fallback-to-build --update-binary
npm error Failed to execute ‘C:\app\node-v22.14.0-win-x64\node.exe C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --update-binary --module=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release\canvas.node --module_name=canvas --module_path=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release --napi_version=10 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v127’ (1)
npm error node-pre-gyp ERR! install response status 404 Not Found on https://github.com/Automattic/node-canvas/releases/download/v2.11.2/canvas-v2.11.2-node-v127-win32-unknown-x64.tar.gz
npm error gyp ERR! find VS
npm error gyp ERR! find VS msvs_version not set from command line or npm config
npm error gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with ‘–loglevel silly’ for more details.
npm error gyp ERR! find VS
npm error gyp ERR! find VS Failure details: undefined
npm error gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with ‘–loglevel silly’ for more details.
npm error gyp ERR! find VS
npm error gyp ERR! find VS Failure details: undefined
npm error gyp ERR! find VS not looking for VS2017 as it is only supported up to Node.js 21
npm error gyp ERR! find VS not looking for VS2017 as it is only supported up to Node.js 21
npm error gyp ERR! find VS not looking for VS2017 as it is only supported up to Node.js 21
npm error gyp ERR! find VS not looking for VS2015 as it is only supported up to Node.js 18
npm error gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm error gyp ERR! find VS
npm error gyp ERR! find VS **************************************************************
npm error gyp ERR! find VS You need to install the latest version of Visual Studio
npm error gyp ERR! find VS including the “Desktop development with C++” workload.
npm error gyp ERR! find VS For more information consult the documentation at:
npm error gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm error gyp ERR! find VS **************************************************************
npm error gyp ERR! find VS
npm error gyp ERR! configure error
npm error gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm error gyp ERR! stack at VisualStudioFinder.fail (C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:118:11)
npm error gyp ERR! stack at VisualStudioFinder.findVisualStudio (C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:17)
npm error gyp ERR! stack at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
npm error gyp ERR! stack at async createBuildDir (C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\lib\configure.js:112:18)
npm error gyp ERR! stack at async run (C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js:81:18)
npm error gyp ERR! System Windows_NT 10.0.26100
npm error gyp ERR! command “C:\app\node-v22.14.0-win-x64\node.exe” “C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js” “configure” “–fallback-to-build” “–update-binary” “–module=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release\canvas.node” “–module_name=canvas” “–module_path=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release” “–napi_version=10” “–node_abi_napi=napi” “–napi_build_version=0” "–node_napi_label=node-v127"npm error gyp ERR! cwd c:\code\trae\nodejs\snow\node_modules\canvas
npm error gyp ERR! node -v v22.14.0
npm error gyp ERR! node-gyp -v v11.0.0
npm error gyp ERR! not ok
npm error node-pre-gyp ERR! build error
npm error node-pre-gyp ERR! stack Error: Failed to execute ‘C:\app\node-v22.14.0-win-x64\node.exe C:\app\node-v22.14.0-win-x64\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --update-binary --module=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release\canvas.node --module_name=canvas --module_path=c:\code\trae\nodejs\snow\node_modules\canvas\build\Release --napi_version=10 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v127’ (1)
npm error node-pre-gyp ERR! stack at ChildProcess. (C:\code\trae\nodejs\snow\node_modules@mapbox\node-pre-gyp\lib\util\compile.js:89:23)
npm error node-pre-gyp ERR! stack at ChildProcess.emit (node:events:518:28)
npm error node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1101:16)
npm error node-pre-gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:304:5)
npm error node-pre-gyp ERR! System Windows_NT 10.0.26100
npm error node-pre-gyp ERR! command “C:\app\node-v22.14.0-win-x64\node.exe” “C:\code\trae\nodejs\snow\node_modules\@mapbox\node-pre-gyp\bin\node-pre-gyp” “install” “–fallback-to-build” “–update-binary”
npm error node-pre-gyp ERR! cwd c:\code\trae\nodejs\snow\node_modules\canvas
npm error node-pre-gyp ERR! node -v v22.14.0
npm error node-pre-gyp ERR! node-pre-gyp -v v1.0.11
npm error node-pre-gyp ERR! not ok
npm error A complete log of this run can be found in: C:\Users\I042416\AppData\Local\npm-cache_logs\2025-08-13T04_19_01_618Z-debug-0.log

安装 canvas 在 Windows 上出错,提示找不到预编译二进制,并缺少编译工具。不用担心,这里简明列出可行解决方案:

必要依赖工具,确保提前准备好:

  • Visual Studio Build Tools(含 Desktop development with C++ 工具集)
    可通过 [node-gyp 文档] 安装(Latenode Official Community)。

  • Python(推荐 2.x 或最新版本),并配置环境变量或用 npm config set python "<路径>" 指定(Codefling)。

  • GTK2 和 Cairo:canvas 在 Windows 上依赖 GTK2 下的 Cairo 图形库。可下载所有‐in‐one GTK2 bundle 解压到 C:\GTK,并将 C:\GTK\bin 加入 PATH(GitHub)。

  • (可选)如果需要 JPEG 支持,还需安装 libjpeg-turbo,默认路径为 C:\libjpeg-turbo(或 64 位对应路径),如非默认位置需用 --jpeg_root 参数指定(GitHub)。

快速安装流程:

  1. 安装以上所有依赖。

  2. 用管理员权限打开 PowerShell。运行:

    npm install -g node-gyp
    npm install canvas
    
    

    或指定 Visual Studio 版本:

    npm install canvas --msvs_version=2022
    
    
  3. 如果仍报错无法加载 Cairo 动态库,可手动将 GTK bin 文件复制到 canvas 模块目录:

    C:\GTK\bin\*.dll -> node_modules\canvas\build\Release
    
    

    这一步有助于解决部分运行时加载问题(delarre.net)。

社区实战经验:

方案总结对比表

场景/状态建议操作
完整构建依赖齐全直接 npm install canvas,正常成功
无预编译二进制被下载(404)准备完整工具链并按上步骤安装
加载 DLL 失败C:\GTK\bin 中 DLL 拷到 build/Release
想快速解决安装 windows-build-tools 后再试
希望使用预编译文件可在 GitHub Releases 查找符合 Node ABI 的版本,下载放入项目中手动加载

这样一来,使用 canvas 时的常见坑都清晰明了。愿你的安装顺利,图形渲染畅通无阻!

// app.js 或页面的 js 文件 import * as THREE from '../../miniprogram_npm/three/index.js'; import { GLTFLoader } from '../../libs/GLTFLoader'; Page({ onReady() { wx.createSelectorQuery() .select('#canvas') .node() .exec((res) => { const canvas = res[0].node; // 兼容微信小程序:补充缺失的 addEventListener 方法 if (!canvas.addEventListener) { canvas.addEventListener = function () {}; } // 补充缺失的 style 对象,避免 setSize 时报错 if (!canvas.style) { canvas.style = {}; } // 获取设备窗口尺寸,并手动设置 canvas 的宽高 const sysInfo = wx.getSystemInfoSync(); canvas.width = sysInfo.windowWidth; canvas.height = sysInfo.windowHeight; const renderer = new THREE.WebGLRenderer({ canvas, context: canvas.getContext('webgl'), antialias: true, }); renderer.setSize(canvas.width, canvas.height); // 创建场景、相机与灯光 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera( 75, canvas.width / canvas.height, 0.1, 1000 ); camera.position.set(0, 1, 3); scene.add(new THREE.AmbientLight(0xffffff, 0.8)); const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8); directionalLight.position.set(5, 10, 7.5); scene.add(directionalLight); // 加载 glTF 模型 const loader = new GLTFLoader(); loader.load( '/assets/2.glb', // 模型文件路径 (gltf) => { scene.add(gltf.scene); }, undefined, (error) => { console.error('加载模型出错:', error); } ); // 动画循环 function animate() { renderer.render(scene, camera); canvas.requestAnimationFrame(animate); } animate(); }); }, }); 微信小程序开发,这段代码报错如下:WAServiceMainContext.js?t=wechat&s=1741278796614&v=3.7.9:1 ReferenceError: Request is not defin
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汪子熙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值