如何解决在MacOS上安装Node `canvas`的问题

1,359 阅读2分钟

我想使用Node.jscanvas NPM包,但运行npm install canvas ,却以这样的信息失败了:

npm ERR! code 1
npm ERR! path /Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas
npm ERR! command failed
npm ERR! command sh -c node-pre-gyp install --fallback-to-build
npm ERR! Failed to execute '/opt/homebrew/Cellar/node@16/16.14.2/bin/node /opt/homebrew/Cellar/node@16/16.14.2/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/Users/flaviocopes/dev/old/generate-images-posts/node_modules/canvas/build/Release --napi_version=8 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v93' (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@16.14.2 | darwin | arm64
npm ERR! node-pre-gyp WARN Using request for node-pre-gyp https download 
npm ERR! node-pre-gyp info check checked for "/Users/flaviocopes/dev/old/generate-images-posts/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-v93-darwin-unknown-arm64.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-v93-darwin-unknown-arm64.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-v93-darwin-unknown-arm64.tar.gz 
npm ERR! node-pre-gyp WARN Pre-built binaries not found for canvas@2.6.1 and node@16.14.2 (node-v93 ABI, unknown) (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-v93-darwin-unknown-arm64.tar.gz 
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@16.14.2 | darwin | arm64

为了解决这个问题,我运行了:

brew install pkg-config cairo pango libpng jpeg giflib librsvg 

npm install canvas ,就可以正常工作了!

还有一件事!⚠️ ✋

在一月底,我将组织网络开发训练营

这是一个为期10周的在线课程,我将指导你成为一名Web开发人员。

这不仅仅是 "一个课程"。它是我每年组织一次的大型活动。

我们将从零开始,学习网络开发的基础知识,HTML,CSS,JavaScript,Tailwind,Node.js,然后我们将学习React,JSX,如何使用PostgreSQL,Astro,Next.js,Prisma等等

在前10周结束时,你将知道如何创建网站和网络应用程序,我将为你解锁Bootcamp的第二阶段:你将获得Bootcamp毕业生专属的大量项目,所以你可以按照我的指示建立像带有认证的私人区域,克隆Twitter YouTube Reddit等流行网站,创建电子商务网站,等等。

因为一旦你掌握了基础知识,你只能通过在真正的、令人兴奋的项目上工作来学习。