Skip to content

[Bug]: Cannot load "@napi-rs/canvas" package: "Error: Cannot find module '@napi-rs/canvas' #19145

Closed
@soadzoor

Description

@soadzoor

Attach (recommended) or Link to PDF file

https://github.com/mozilla/pdf.js/files/13179307/c53a656b-551d-4c24-bfb6-f994ef3a6d4b.pdf

But I believe it has nothing to do with the PDF file itself.

Web browser and its version

Node.js v22.11.0

Operating system and its version

Ubuntu 22.04.2 LTS (but I tried with newest MacOS as well, same issue...)

PDF.js version

4.9.124

Is the bug present in the latest PDF.js version?

No

Is a browser extension

No

Steps to reproduce the problem

  1. Clone this repo: https://github.com/soadzoor/node-pdfjs
  2. Run npm install
  3. Run node index.js

What is the expected behavior?

It should generate a PNG file

What went wrong?

It generates a runtime error when running node index.js:
Unfortunately, it gives me a runtime error:
node index.js
Warning: Cannot load "@napi-rs/canvas" package: "Error: Cannot find module '@napi-rs/canvas'
Require stack:

  • /home/runner/work/pdf.js/pdf.js/src/display/node_utils.js".
    Warning: Cannot polyfill DOMMatrix, rendering may be broken.
    Warning: Cannot polyfill ImageData, rendering may be broken.
    Warning: Cannot polyfill Path2D, rendering may be broken.
    PDF loaded
    Page loaded
    node:internal/modules/cjs/loader:1249
    const err = new Error(message);
    ^

Error: Cannot find module '@napi-rs/canvas'
Require stack:

  • /home/runner/work/pdf.js/pdf.js/src/display/node_utils.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
    at Function._load (node:internal/modules/cjs/loader:1075:27)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:141:16)
    at NodeCanvasFactory._createCanvas (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/legacy/build/pdf.mjs:10769:20)
    at NodeCanvasFactory.create (file:///home/soadzoor/works/node-pdfjs/node_modules/pdfjs-dist/legacy/build/pdf.mjs:10241:25)
    at file:///home/soadzoor/works/node-pdfjs/index.js:37:46 {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ '/home/runner/work/pdf.js/pdf.js/src/display/node_utils.js' ]
    }

Node.js v22.11.0

Link to a viewer

No response

Additional context

I was happy to learn that pdfjs replaced node-canvas with a new canvas implementation (@napi-rs/canvas) which has much less dependencies. This fact also gave me hope that the bug I reported a long time ago might be fixed now, so I gave it a shot.

First I created the canvas context like this:

import {createCanvas} from "@napi-rs/canvas";

var canvas = createCanvas(2048, 2048);
var context = canvas.getContext('2d');

Then I checked the official examples, and based on those, I replaced those with these lines below, and removed the @napi-rs/canvas dependency from my package.json, deleted node_modules and ran npm install again:

const canvasFactory = pdf.canvasFactory;
const canvasAndContext = canvasFactory.create(2048, 2048);

Unfortunately, I still have the same issue.

What am I doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions