如何解决npm install中的错误?

18

我是一名初学者程序员,正在使用node和discord.js制作discord机器人。我需要一个名为canvas的库,但无法安装!

我在我的Windows PC上编程和测试了这个应用程序,然后将其发送到我的树莓派服务器上。它没有工作,所以我尝试删除并重新安装库,但在安装时出现以下错误:

(我正在尝试使用此命令进行安装:npm install canvas)

npm ERR! code 1
npm ERR! path /home/pi/node/test/node_modules/canvas
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
npm ERR! Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/home/pi/node/test/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/home/pi/node/test/node_modules/canvas/build/Release --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v88' (1)
npm ERR! node-pre-gyp info it worked if it ends with ok
npm ERR! node-pre-gyp info using node-pre-gyp@0.11.0
npm ERR! node-pre-gyp info using node@15.0.1 | linux | arm
npm ERR! node-pre-gyp WARN Using needle for node-pre-gyp https download 
npm ERR! node-pre-gyp info check checked for "/home/pi/node/test/node_modules/canvas/build/Release/canvas.node" (not found)
npm ERR! node-pre-gyp http GET https://github.com/node-gfx/node-canvas-prebuilt/releases/download/v2.6.1/canvas-v2.6.1-node-v88-linux-glibc-arm.tar.gz
npm ERR! node-pre-gyp http 404 https://github.com/node-gfx/node-canvas-prebuilt/releases/download/v2.6.1/canvas-v2.6.1-node-v88-linux-glibc-arm.tar.gz
npm ERR! node-pre-gyp WARN Tried to download(404): https://github.com/node-gfx/node-canvas-prebuilt/releases/download/v2.6.1/canvas-v2.6.1-node-v88-linux-glibc-arm.tar.gz 
npm ERR! node-pre-gyp WARN Pre-built binaries not found for canvas@2.6.1 and node@15.0.1 (node-v88 ABI, glibc) (falling back to source compile with node-gyp) 
npm ERR! node-pre-gyp http 404 status code downloading tarball https://github.com/node-gfx/node-canvas-prebuilt/releases/download/v2.6.1/canvas-v2.6.1-node-v88-linux-glibc-arm.tar.gz 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@15.0.1 | linux | arm
npm ERR! gyp info ok 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@15.0.1 | linux | arm
npm ERR! gyp info find Python using Python version 3.7.3 found at "/usr/bin/python3"
npm ERR! gyp info spawn /usr/bin/python3
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   '/usr/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   '/home/pi/node/test/node_modules/canvas/build/config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/usr/lib/node_modules/npm/node_modules/node-gyp/addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   '/home/pi/.cache/node-gyp/15.0.1/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=/home/pi/.cache/node-gyp/15.0.1',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=/usr/lib/node_modules/npm/node_modules/node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=/home/pi/.cache/node-gyp/15.0.1/<(target_arch)/node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=/home/pi/node/test/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 pixman-1 was not found in the pkg-config search path.
npm ERR! Perhaps you should add the directory containing `pixman-1.pc'
npm ERR! to the PKG_CONFIG_PATH environment variable
npm ERR! No package 'pixman-1' found
npm ERR! gyp: Call to 'pkg-config pixman-1 --libs' returned exit status 1 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/lib/node_modules/npm/node_modules/node-gyp/lib/configure.js:351:16)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:327:20)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:277:12)
npm ERR! gyp ERR! System Linux 5.4.51-v7l+
npm ERR! gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/home/pi/node/test/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/home/pi/node/test/node_modules/canvas/build/Release" "--napi_version=7" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v88"
npm ERR! gyp ERR! cwd /home/pi/node/test/node_modules/canvas
npm ERR! gyp ERR! node -v v15.0.1
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok 
npm ERR! node-pre-gyp ERR! build error 
npm ERR! node-pre-gyp ERR! stack Error: Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/home/pi/node/test/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/home/pi/node/test/node_modules/canvas/build/Release --napi_version=7 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v88' (1)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/home/pi/node/test/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:327:20)
npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1048:16)
npm ERR! node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:288:5)
npm ERR! node-pre-gyp ERR! System Linux 5.4.51-v7l+
npm ERR! node-pre-gyp ERR! command "/usr/bin/node" "/home/pi/node/test/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd /home/pi/node/test/node_modules/canvas
npm ERR! node-pre-gyp ERR! node -v v15.0.1
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v0.11.0
npm ERR! node-pre-gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2020-10-27T20_10_02_765Z-debug.log```
5个回答

28

首先,尝试安装canvas二进制文件,请参考https://github.com/Automattic/node-canvas中的说明。

sudo apt-get install build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev

然后安装canvas:

npm install canvas

如何在Mac上操作? - Arpit Bhalla
在Windows中该怎么做? - Shehzad Ahmed
1
我的组织Ubuntu虚拟机因为这个画布错误而崩溃了。如果不是这个答案,它就会彻底瘫痪。非常感谢您的回答。 - jnsnsml

19
你是不是从运行npm init开始的?我猜是因为错误中提到了node_modules文件夹,但还是要确认一下。
你可以使用homebrew安装它:
brew install pkg-config cairo pango libpng jpeg giflib librsvg

根据这里的文档:https://github.com/Automattic/node-canvas

1
他们没有使用MacOS。 - Quentin
他们也没有使用Windows。 - Quentin
你说的是Windows,不是Linux!(在跟随链接并滚动一段时间后,很明显brew不支持ARM处理器) - Quentin
是的:“尽力而为”的基础。即不得到适当支持。 - Quentin
谢谢您的快速回复,虽然它没有解决我的问题,但今天我从头开始启动了一个新项目,进行了npm init,并安装了所有库。它成功了!可能是由于从Windows转换到Linux时出现了某些不兼容性问题! - Samu Nemeth
哟,我在这里找到了“梅林”。运行得很顺畅!谢谢。 - Buchi

18

在我的 Macbook M1 Pro 上可行 -> arch -arm64 brew install pkg-config cairo pango jpeg giflib librsvg


1
正如有人指出的那样,问题的作者并没有使用macOS。否则,在M1 Pro上,几乎没有人提供预编译的二进制文件,因此必须重新编译所有本地Node.js插件 - 主要是因为Github Actions尚不支持它。 - mmomtchev
5
作为一位使用 M1 Mac 的用户,这实际上解决了我在 npm 安装时遇到的问题。谢谢! - Mauvis Ledford
1
在我的Macbook Pro上也可以运行。使用以下命令进行安装:brew install pkg-config cairo pango jpeg giflib librsvg - Tom Dhanabhon
对我来说也适用于M1。 - Arpit Bhalla
对于 M1,这对我也有效。 - Arpit Bhalla

1
在我的情况下,解决问题的方法是使用 Node.js v14。
nvm use 14

0
对于那些自酿失败的人来说:
brew install pkg-config cairo pango libpng jpeg giflib librsvg

我找到了原因,是因为在安装Homebrew后,它没有正确配置pkg-config。我正在使用搭载M1芯片的Mac OSX。
你需要安装Mac Ports,然后运行。
sudo port install pkgconfig pango

这是一个漫长的过程,可能需要1小时或更长时间,但它可以解决“npm ERR!找不到包'pixman-1'”的问题。
这个和这个都是从这个github问题中获取的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接