Node Canvas throwing error when running project

Viewed 3550

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.

2 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.

I use npm install --build-from-source. It work!

But there are new mistakes

dlopen(.../node_modules/canvas/build/Release/canvas.node, 0x0001): symbol not found in flat namespace '_cairo_fill'

Related