yarn或npm新增canvas组件不成功的解决办法

各种办法用尽,还是官方的方法最好用。

官方地址:Installation: Windows · Automattic/node-canvas Wiki · GitHub

Installation: Windows

Dependencies

Building the node-canvas module requires:

  1. A global installation of node-gyp.
  2. GTK 2
  3. For optional JPEG support (node-canvas 2.0 and later): libjpeg-turbo

Install Manually

1. Installing node-gyp

Follow the instructions here: https://github.com/nodejs/node-gyp#on-windows

2. Installing GTK 2

You will need the cairo library which is bundled in GTK. Download the GTK 2 bundle for Win32 or Win64. Unzip the contents in C:\GTK.

Notes:

  • Both GTK and Node.js need either be 64bit or 32bit to compile successfully.
  • Download GTK 2, not GTK 3, which is missing the required libpng. If you get linker errors you’ve most likely picked the wrong bundle.
  • If you use a different location than C:\GTK, add a GTK_Root argument to npm install or node-gyp rebuild. For example: node-gyp rebuild --GTK_Root=C:\somewhere\GTK.

3. Installing libjpeg-turbo (optional, for JPEG support; node-canvas 2.0 and later)

Download the latest libjpeg-turbo SDK for Visual C++ (currently libjpeg-turbo-2.0.0-vc.exe or libjpeg-turbo-2.0.0-vc64.exe) and install to its default location (C:\libjpeg-turbo if 32bit or C:\libjpeg-turbo64 if 64bit).

Notes:

  • Both libjpeg-turbo and Node.js need either be 64bit or 32bit to compile successfully.
  • If you use a different location, add a jpeg_root argument to npm install or node-gyp rebuild. For example: node-gyp rebuild --jpeg_root=C:\somewhere\libjpeg-turbo.

4. Installing node-canvas

After all dependencies are setup, npm install canvas or yarn add canvas.

Install with Chocolatey

⚠️ Be aware that according to known Chocolatey issues it is better to use instructions for manual install, provided above. If you figure out a way to fix the chocolatey instructions, feel free to edit this wiki.

All the dependencies can be installed with Chocolatey, the package manager for Windows.

  1. choco install -y python2 gtk-runtime microsoft-build-tools libjpeg-turbo (may require sudo/Administrator and reboot).
  2. You will need the cairo library which is bundled in GTK. Download the GTK 2 bundle for Win32 or Win64. Unzip the contents in C:\GTK.
  3. Run npm install canvas or yarn add canvas with command line from your project folder.
Linux系统内核配置问题导致…