Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Canvas throwing error when running project

Node Canvas is not working for me on m1

When I try to run my project I get this error message:

Error: dlopen(.../node_modules/canvas/build/Release/canvas.node, 0x0001): tried: '.../node_modules/canvas/build/Release/canvas.node' (mach-o file, but is an incompatible architecture (have 'arm64', need 'x86_64')), '/usr/local/lib/canvas.node' (no such file), '/usr/lib/canvas.node' (no such file)

Already did:

brew install pkg-config cairo pango libpng jpeg giflib librsvg

xcode-select --install

npm i canvas

With no errors

Not sure how to debug this as there are people with Node Canvas working on m1 laptops, please help.

like image 542
Blaze Cerovcec Avatar asked May 18 '26 13:05

Blaze Cerovcec


1 Answers

The most important thing is to make sure your Node executable has been compiled for Apple Silicon, meaning there's a pre-built binary for darwin-arm64. You can see which versions have which binaries here (the following page has the binaries for Node version 16.13.1): https://nodejs.org/download/release/v16.13.1/.

Node v14 has no pre-built binaries for darwin-arm64 (see https://github.com/nodejs/node/issues/36161), because it can't be compiled natively to Apple Silicon.

I use volta (https://volta.sh/) to manage my Node versions, so if you install Node v16 with volta install node@16 and then run npm i canvas, you should get a correctly compiled canvas.node file.

If that fails, go to node_modules/canvas and run npm install --build-from-source, which will compile the C++ addon and output the corect canvas.node.

You can check this by running:

file node_modules/canvas/build/Release/canvas.node
# outputs: node_modules/canvas/build/Release/canvas.node: Mach-O 64-bit bundle arm64

If it says arm64 at the end, then you're good to go. If it still says x86_64, then something went wrong.

like image 126
apollo9981 Avatar answered May 20 '26 05:05

apollo9981



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!