Skip to content

Resolve the pdf.js worker explicitly so it survives bundling - #5

Merged
govindup63 merged 1 commit into
mainfrom
fix/pdfjs-worker-resolution
Aug 22, 2026
Merged

govindup63 merged 1 commit into
mainfrom
fix/pdfjs-worker-resolution

Conversation

@govindup63

Copy link
Copy Markdown
Contributor

The bug

Deploying a consumer of this library to a serverless host fails on the first redaction:

Setting up fake worker failed: "Cannot find module
'/var/task/node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs'
imported from /var/task/node_modules/pdfjs-dist/legacy/build/pdf.mjs"

Under Node, pdf.js runs the worker in-process and reaches it with a dynamic import() of a path it computes at runtime. Nothing static points at that file, so a bundler or a file tracer cannot see the dependency, and the file never makes it into the deployment. It works locally only because the guessed path happens to be right when the whole node_modules tree is present.

The fix

Set GlobalWorkerOptions.workerSrc from require.resolve with a literal specifier. Tracers follow require.resolve() on a static string, so the worker now gets traced and shipped like any other file. Behaviour is unchanged locally, where the path pdf.js guessed was already correct.

Still on the consumer

standard_fonts/ and cmaps/ are handed to pdf.js as directories, not files, so there is no specifier to resolve and no way to make them statically visible from here. A serverless deployment has to include them explicitly, or a PDF that relies on a standard or CID-keyed font renders as empty boxes rather than failing loudly. That is called out in the commit message so the next person hits it as a documented caveat instead of a mystery.

Tests 14/14, typecheck clean.

Under Node, pdf.js runs its worker in-process and reaches it with a
dynamic import() of a path it works out for itself. Nothing static points
at that file, so a bundler or a file tracer cannot see it, and a
serverless build leaves it out of the deployment. The first getDocument
then fails with:

  Setting up fake worker failed: Cannot find module
  '/var/task/node_modules/pdfjs-dist/legacy/build/pdf.worker.mjs'

Setting GlobalWorkerOptions.workerSrc from require.resolve with a literal
specifier makes the dependency statically analysable, so it gets traced
and shipped like any other file. No behaviour change locally, where the
guessed path was already correct.

Note for consumers: standard_fonts/ and cmaps/ are still reached through a
computed path, because pdf.js wants a directory rather than a file. A
serverless deployment has to include those explicitly or CID-keyed and
standard-font PDFs render as empty boxes.

Signed-off-by: Govind Pandey <govindup63@gmail.com>
@govindup63
govindup63 merged commit 12ab438 into main Aug 22, 2026
6 checks passed
@govindup63
govindup63 deleted the fix/pdfjs-worker-resolution branch August 22, 2026 05:43
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 this pull request may close these issues.

1 participant