denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
97.15k stars 5.36k forks source link

node-canvas doesn't work #25974

Closed alexgleason closed 4 weeks ago

alexgleason commented 4 weeks ago

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: https://github.com/DjDeveloperr/deno-canvas/issues/40 https://github.com/DjDeveloperr/skia_canvas/issues/66 https://github.com/DjDeveloperr/skia_canvas/issues/68 https://github.com/DjDeveloperr/skia_canvas/issues/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 commented 4 weeks ago

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 rimraf@3.0.2 is deprecated: Rimraf versions prior to v4 are no longer supported
Warning npmlog@5.0.1 is deprecated: This package is no longer supported.
Warning inflight@1.0.6 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 glob@7.2.3 is deprecated: Glob versions prior to v9 are no longer supported
Warning gauge@3.0.2 is deprecated: This package is no longer supported.
Warning are-we-there-yet@2.0.0 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:canvas@2.11.2

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

alexgleason commented 4 weeks ago

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 commented 4 weeks ago

@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