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
- Move canvas to optionalDependencies, which enables npm to continue installing pdfjs-dist even if the installation of canvas fails. Close #15652 — committed to tamuratak/pdf.js by tamuratak 4 years ago
- Test merge with squash (#32) * Use a consistent `outline` for all UI buttons (PR 15438 follow-up) Currently the `viewBookmark`-button, which is actually a `href`-element, gets an inconsistent `out... — committed to MasterControlIncPublic/pdf.js by shaymerrill-mc 4 years ago
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
canvasandpath2ddependencies in favor of@napi-rs/canvas. This will be part of the upcoming release and hopefully resolves the installation issues that were experienced with thecanvaspackage.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-distdoesn’t seem to work. Thecanvasdependency is still installed and appears innode_modulesandpackage-lock.json. I use electon.js and electron-rebuild to build my electron app and electron-rebuild tries to rebuild thecanvasfrom the node modules which I don’t event need.One of options would be moving
canvasintooptionalDependenciesfromdependencies. Then,npm i pdfjs-distwill installcanvasas a dependency by default, andnpmwill not stop installingpdfjs-disteven if installingcanvasfails.Users also can execute
npm i --omit=optional pdfjs-distif they want not to installcanvas.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:
Since
canvas, is an optional dependency, an override to a non existing location is ignored silently.canvasmodule does not work inside worker threads, which are in use by testing frameworks likevitest.Can it be dropped or be made optional so the consumer can provide it to pdf.js on-demand? At minimum, the
requirefor it should be wrapped in atry-catchclause.Can we please reopen / reconsider this? By now
cavasis 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
canvasdep with the package you listed above?omitworks (best set in.npmrc), but it’s dangerous because it will omit all optional dependencies from all your dependencies, not justpdfjs-dist.So by using
optionalDependencieswe 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.