user12781352
user12781352

Reputation:

Cannot install canvas on npm

I am trying to install canvas on my raspberry pi via npm after successfully installing on my main pc, but now I get a really long error that I don't know how to fix.
Here's the full error:

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/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 Linux 5.15.32+
npm ERR! gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--update-binary" "--module=/home/alex/discord-bot/node_modules/canvas/build/Release/canvas.node" "--module_name=canvas" "--module_path=/home/alex/discord-bot/node_modules/canvas/build/Release" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v93"
npm ERR! gyp ERR! cwd /home/alex/discord-bot/node_modules/canvas
npm ERR! gyp ERR! node -v v16.16.0
npm ERR! gyp ERR! node-gyp -v v9.0.0
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/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --update-binary --module=/home/alex/discord-bot/node_modules/canvas/build/Release/canvas.node --module_name=canvas --module_path=/home/alex/discord-bot/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 ERR! stack     at ChildProcess.<anonymous> (/home/alex/discord-bot/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js:89:23)
npm ERR! node-pre-gyp ERR! stack     at ChildProcess.emit (node:events:527:28)
npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1092:16)
npm ERR! node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5)
npm ERR! node-pre-gyp ERR! System Linux 5.15.32+
npm ERR! node-pre-gyp ERR! command "/usr/local/bin/node" "/home/alex/discord-bot/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build" "--update-binary"
npm ERR! node-pre-gyp ERR! cwd /home/alex/discord-bot/node_modules/canvas
npm ERR! node-pre-gyp ERR! node -v v16.16.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.9
npm ERR! node-pre-gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/alex/.npm/_logs/2022-08-09T20_08_23_632Z-debug-0.log

I used the command npm install --build-from-source canvas, please can I have help?

Upvotes: 16

Views: 47124

Answers (9)

Albert Renshaw
Albert Renshaw

Reputation: 17902

If you have tried the brew dependencies listed in other solutions and it still doesn't work you are likely on an old version of node... upgrading fixed everything for me.

$ nvm install --lts

$ nvm use --lts

$ npm i canvas

Cheers

(Note: You may wish to update your system usage of node to the latest rather than just this session in terminal $ nvm alias default "lts/*" )

Upvotes: 0

Shuwei
Shuwei

Reputation: 791

For me the solution is to use python 3.10.0, I was using python 3.10.14, which is the default version of python 3.10 if you install python with homebrew. After I specifically installed python 3.10.0, the error is gone and installation goes through. I’m on a 2019 intel chip mac

Upvotes: 1

Meir Pechthalt
Meir Pechthalt

Reputation: 313

My case:

MacOS Sonoma 14.5 failed npm install canvas.

After digging in the error log, root cause were errors similar to error <cstddef> tried including <stddef.h> but didn't find libc++'s <stddef.h> header..

Solution was:

export SDKROOT=$(xcrun --show-sdk-path)
npm install canvas

Upvotes: 0

Alexandr
Alexandr

Reputation: 11

You need to install these modules (see canvas readme)

brew install pkg-config cairo pango libpng jpeg giflib librsvg pixman

If you have an error like import gyp not found, you can try this

python3 -m pip install gyp-next

Upvotes: 1

Lexa
Lexa

Reputation: 197

1 Install Dependencies: Install the required dependencies using Homebrew. Open a terminal and run the following commands:

brew install pkg-config cairo pango libpng jpeg giflib

2 Set PKG_CONFIG_PATH: Export PKG_CONFIG_PATH to include the directory containing pixman-1.pc. Add the following line to your shell profile file (e.g., ~/.zshrc or ~/.bashrc):

export PKG_CONFIG_PATH="/opt/homebrew/opt/pixman/lib/pkgconfig"

Then, restart your terminal or run:

source ~/.zshrc # or source ~/.bashrc if you are using bash

3 Reinstall Node Modules: Remove the node_modules directory and package-lock.json file from your project and then reinstall the dependencies:

rm -rf node_modules package-lock.json npm install

Upvotes: 3

Dave Lee
Dave Lee

Reputation: 524

In my case it's resolved by manually installing packages inside of canvas.

Apparently if you set NPM to ignore scripts, which prevents node-pre-gyp from downloading prebuilt binaries. I accessed to canvas folder in node_modules and then manually run node-gyp rebuild and then it worked.

Upvotes: 0

Supawat Pothisomphop
Supawat Pothisomphop

Reputation: 291

Try this before run npm install canvas. It work for me

brew install pkg-config cairo pango libpng jpeg giflib librsvg 

Ref : https://flaviocopes.com/fix-node-canvas-error-pre-gyp-macos/

Upvotes: 29

Nwawel A Iroume
Nwawel A Iroume

Reputation: 1379

You have to install required dependencies for compiling your system from https://www.npmjs.com/package/canvas

For ubuntu i did

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

Upvotes: 2

MKEY
MKEY

Reputation: 146

Seems you're missing the 'pixman' dependency. Did you follow the guide to install all the needed dependencies before installing canvas? It requires a bunch of dependencies.

Upvotes: 13

Related Questions