Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node-canvas doesn't work #25974

Closed
alexgleason opened this issue Oct 1, 2024 · 3 comments · Fixed by #25975
Closed

node-canvas doesn't work #25974

alexgleason opened this issue Oct 1, 2024 · 3 comments · Fixed by #25975

Comments

@alexgleason
Copy link
Contributor

Version: Deno 1.46.3

When trying to import node-canvas:

> import { createCanvas } from 'npm:canvas';
Uncaught Error: Cannot find module '../build/Release/canvas.node'
Require stack:
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/canvas.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/index.js
- /home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/index.js
    at Function.Module._resolveFilename (node:module:608:15)
    at Function.Module._load (node:module:486:27)
    at Module.require (node:module:674:19)
    at require (node:module:801:16)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js:3:18)
    at Object.<anonymous> (file:///home/alex/.cache/deno/npm/registry.npmjs.org/canvas/2.11.2/lib/bindings.js:15:4)
    at Module._compile (node:module:736:34)
    at Object.Module._extensions..js (node:module:757:11)
    at Module.load (node:module:655:32)
    at Function.Module._load (node:module:523:13)

Why not use deno-canvas or skai_canvas instead? Because I am running into all kinds of issues: DjDeveloperr/deno-canvas#40 DjDeveloperr/skia_canvas#66 DjDeveloperr/skia_canvas#68 DjDeveloperr/skia_canvas#69

it's basically impossible to use a canvas library in Deno right now, and I thought with Node support node-canvas would be a last ditch effort, but something is blocking it from working here.

@bartlomieju
Copy link
Member

This package relies on native Node addons (https://docs.deno.com/runtime/fundamentals/node/#node-api-addons) and thus require node_modules directory to be present. Additionally this library requires a lifecycle script to set up these bindings.

You can fix the problem by running with --node-modules-dir flag or by putting { "nodeModulesDir": true } in your config file. (Btw, in Deno 2 these will be --node-modules-dir=auto and { "nodeModulesDir": "auto" }). You might also need to specify --allow-scripts=npm:canvas.

import { createCanvas } from 'npm:canvas';
$ deno run -A --node-modules-dir main.js
Warning [email protected] is deprecated: Rimraf versions prior to v4 are no longer supported
Warning [email protected] is deprecated: This package is no longer supported.
Warning [email protected] is deprecated: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
Warning [email protected] is deprecated: Glob versions prior to v9 are no longer supported
Warning [email protected] is deprecated: This package is no longer supported.
Warning [email protected] is deprecated: This package is no longer supported.
warning: Packages contained npm lifecycle scripts (preinstall/install/postinstall) that were not executed.
    This may cause the packages to not work correctly. To run them, use the `--allow-scripts` flag with `deno cache`
    (e.g. `deno cache --allow-scripts=pkg1,pkg2 <entrypoint>`):
      npm:[email protected]

I'll try to improve the error message to provide a helpful message here.

@alexgleason
Copy link
Contributor Author

Thank you very much @bartlomieju. I really don't want to have node_modules in my project, so I will have to fork one of these canvas repos. This canvas ecosystem needs to be pushed forward a bit.

@crowlKats
Copy link
Member

@alexgleason just for info: hopefully in the coming months we will have OffscreenCanvas in Deno; it will not have 2DContext initially, but it is something we will add later on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants