pdf.js: `canvas` should not be a dependency if possible

canvas has been added to the dependency, not devDependency, since v3.0.279. I don’t think it is a good idea. Because node-canvas prebuilds aren’t available for ARM, users with macOS on ARM will request helps from you, which would increase the maintenance burden. We can say the same thing for users with old Linux for which node-canvas prebuilds aren’t available.

When GitHub actions supports macOS on ARM, the situation would change.

  "dependencies": {
    "canvas": "^2.10.1",
    "web-streams-polyfill": "^3.2.1"
  },

Configuration:

  • PDF.js version: 3.0.279

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 15
  • Comments: 17 (3 by maintainers)

Commits related to this issue

Most upvoted comments

Is there any way to reduce the needed size of the canvas package? when I use pdfjs in a nextjs environment, the canvas package is like 48mb and the limit for Vercel is 50mb! This makes it incredibly difficult to build an app. No other package is even close to this size. Kind of pulling my hair out over this, i currently cant deploy my app. Any ideas?

In PR #19015 we removed the canvas and path2d dependencies in favor of @napi-rs/canvas. This will be part of the upcoming release and hopefully resolves the installation issues that were experienced with the canvas package.

I wonder if using @napi-rs/canvas instead would be a good solution for both M1 and Vercel environments.

The npm i --omit=optional pdfjs-dist doesn’t seem to work. The canvas dependency is still installed and appears in node_modules and package-lock.json. I use electon.js and electron-rebuild to build my electron app and electron-rebuild tries to rebuild the canvas from the node modules which I don’t event need.

One of options would be moving canvas into optionalDependencies from dependencies. Then, npm i pdfjs-dist will install canvas as a dependency by default, and npm will not stop installing pdfjs-dist even if installing canvas fails.

Users also can executenpm i --omit=optional pdfjs-dist if they want not to install canvas.

At the end, I have to come back at what I’ve written in an above comment. --omit=optional didn’t work as other dependencies were marked as optional (esbuild-*) but were necessary anyway. I am using another solution which also looks like a hack but seems to work. It comes from this SO answer: https://stackoverflow.com/a/73846274. It consists in adding this to package.json:

"overrides": {
    "canvas": "../_EXCLUDED_"
  }

Since canvas, is an optional dependency, an override to a non existing location is ignored silently.

canvas module does not work inside worker threads, which are in use by testing frameworks like vitest.

Can it be dropped or be made optional so the consumer can provide it to pdf.js on-demand? At minimum, the require for it should be wrapped in a try-catch clause.

Can we please reopen / reconsider this? By now cavas is clearly in a dying state: For many platforms it’s not even installable anymore:

Since this currently impacts pdf.js (currenty forced to use pre-release code to get it to work: https://github.com/mozilla/pdf.js/pull/18922) there should be a documented way to skip this dependency (if it needs be be in there for a transition period)

@net how would you go about replacing the canvas dep with the package you listed above?

omit works (best set in .npmrc), but it’s dangerous because it will omit all optional dependencies from all your dependencies, not just pdfjs-dist.

So by using optionalDependencies we basically change an install-time error into a potential run-time error for some (hopefully small number of) Node.js users, while avoiding any issues for browser users? If so, I suppose that we can try that; please submit a patch.