oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.26k stars 2.69k forks source link

Support `node-canvas` #5835

Open robobun opened 1 year ago

robobun commented 1 year ago

To use node-canvas in Bun, upgrade to node-canvas's v3:

bun add node-canvas@next
npm add node-canvas@next

node-canvas@latest does not currently work with Bun as it relies on some v8-specific APIs in Node

We may eventually implement these: https://github.com/oven-sh/bun/issues/3110

Or perhaps it would be easier to upstream a fix

This also blocks jsdom

colinhacks commented 1 year ago

Related to https://github.com/oven-sh/bun/issues/3110

Original issue:


Hello! I've installed canvas on my project and seem to be getting errors whenever I use it. I created a repository with the bare code required to create the error.

Code:

import * as canvas from "canvas";

(async () => {
    canvas.loadImage("https://static.bunnycdn.ru/i/cache/images/3/37/375e7fdc580766874948e5bf678e2a8a.jpg").then((image) => {
        console.log("Loaded!");

        const c = canvas.createCanvas(image.width, image.height);
        const context = c.getContext('2d');
        context.drawImage(image, 0, 0);
    })
});

Error:

error: Cannot find module "../build/Release/canvas.node" from "/Users/eltik/Documents/Coding/Bun-canvas/node_modules/canvas/lib/bindings.js"
error: script "start" exited with code 1 (SIGHUP)

Repository: https://github.com/Eltik/Bun-canvas

Originally reported on Discord: Incompatibility with Canvas

SalahEddin commented 11 months ago

Having the same issue in my Nuxt application, when I try to deserialise my imageDataUrl on the API server

Whobeu commented 9 months ago

I have been playing around with Bun since about 1.0.2 and node-canvas is a package I use extensively for generating server side charts through Chart.js. For modules that do not require canvas, Bun is great and I love being able to run Typescript modules directly without transpiling. But most of the projects are Chart.js based. I understand there are two issues with node-canvas support but the one I run into is the lack of support for the node_module_register API entry point. The other issues reported is related to the npm registry and a background update.

siinghd commented 8 months ago

Any update on this? facing the same error

bun --watch index.ts
error: Cannot find module "../build/Release/canvas.node" from "/Users/harpreetsingh/Projects/imageoptimizer/node_modules/canvas/lib/bindings.js"
medzhidov commented 7 months ago

I have the same error with just installed canvas: CleanShot 2024-01-27 at 13 05 19

btakita commented 7 months ago

Are there any working alternatives in the mean time?

andres-carvajal commented 7 months ago

Facing same issue on Bun using ascii-art package

chriskrogh commented 7 months ago

+1 here. support for canvas is the last holdout for us to adopt bun install in @faire's frontend monorepo.

btakita commented 7 months ago

FWIW there is canvaskit-wasm.

chriskrogh commented 7 months ago

FWIW there is canvaskit-wasm.

some of our modules (including jsdom) try to link canvas without actually depending on it. is there a way to get them to link canvaskit-wasm instead of canvas? if not, I think we need to wait for support

btakita commented 7 months ago

@chriskrogh Would assigning the canvas api from canvis-kit onto globalThis work?

chriskrogh commented 7 months ago

@chriskrogh Would assigning the canvas api from canvis-kit onto globalThis work?

I'm not sure when I would do that? this issue arises early on in the installation process and aborts bun install entirely (node_modules aren't generated)

devaoto commented 3 months ago

When are we gonna have this?

arthurwolf commented 3 months ago

I think I'm hitting the same thing, I have a project that works with node and when I try to use bun instead I get:

╰─(base) ⠠⠵ bun run  src/scripts/process_scenes.ts --loop                                                     on master|✚2-29…5
bun: symbol lookup error: /home/arthur/dev/ai/manga/node_modules/opencv4nodejs/build/Release/opencv4nodejs.node: undefined symbol: node_module_register

Is this the correct issue to report this / follow, or should I file a separate issue ?

Thanks.

hushoca commented 3 months ago

is this likely to be resolved soon?

alexdsgmoura commented 3 months ago

and this error?

root@celestrox:~/celestrox/backend# bun run ./src/server.ts bun: symbol lookup error: /root/celestrox/backend/node_modules/cpu-features/build/Release/cpufeatures.node: undefined symbol: node_module_register

blueonrails commented 3 months ago

I got the same error trying to move to Bun. I have several projects with C Bidings libs, It would be awesome to have this support.

ignSKRRRTT commented 3 months ago

Bun recode of my API currently halted because of this issue :(

ignSKRRRTT commented 3 months ago

For anyone looking for an alternative, I was able to use @napi-rs/canvas. It was basically a drag and drop replacement other than a few lines.

maietta commented 3 months ago

Thank you, @ignSKRRRTT!

Got a puzzle game image generator that I could not continue work on until this was figured out.

Jarred-Sumner commented 2 months ago

This is blocked on #4290 or https://github.com/Automattic/node-canvas/pull/2235 (node-canvas 3.0), whichever happens first

As of node-canvas v2.11.2, here are the list of symbols we would need to implement in order to support this version:

devaoto commented 2 months ago

It is gonna take a year to implement all these

LordFarquaadtheCreator commented 1 month ago

I have the same error with just installed canvas: CleanShot 2024-01-27 at 13 05 19

got the same error, July 31st error: Cannot find module "../build/Release/canvas.node" from "/home/farquaad/xxx/xxx/xxx/node_modules/canvas/lib/bindings.js"

Sayrix commented 3 weeks ago

Hi @robobun the module that you mean is canvas, not node-canvas, so it's not bun add node-canvas@next but bun add canvas@next