Skip to content

Fix a CommonJS bundle of the Node build throwing on load on Windows - #1828

Merged
blikblum merged 1 commit into
foliojs:masterfrom
b84330808:fix-windows-cjs-bundle-load
Oct 23, 2026
Merged

blikblum merged 1 commit into
foliojs:masterfrom
b84330808:fix-windows-cjs-bundle-load

Conversation

@b84330808

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Bug fix, follow-up to #1802 (not released yet).

When a bundler folds the Node build into a CommonJS file, import.meta.url is undefined and lib/document.node.js falls back to createRequire('file:///'). That URL has no drive letter, so on Windows createRequire throws while the bundle is loading:

TypeError [ERR_INVALID_ARG_VALUE]: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received 'file:///'

Repro on Windows with master packed via npm pack:

// index.mjs
import PDFDocument from 'pdfkit';
console.log(typeof PDFDocument);

npx esbuild index.mjs --bundle --platform=node --format=cjs --outfile=dist/index.cjs
node dist/index.cjs

The fallback is now /, which is absolute on every platform. On Linux and macOS it resolves the same way file:/// did. On Windows the bundle now loads, an unregistered standard font still fails with the "Cannot load the standard font" error, and fonts passed to registerStdFonts() render.

The "as CommonJS" cases in tests/unit/bundling.spec.js already cover this. They fail on Windows without this change and pass with it; CI only runs on Ubuntu, so it didn't show up there.

Checklist:

I used Claude Code to write this change.

When a bundler folds the Node build into a CommonJS file, `import.meta.url`
is undefined and `createRequire` falls back to `file:///`. That URL has no
drive letter, so on Windows `createRequire` throws ERR_INVALID_ARG_VALUE while
the bundle is being loaded, before any document asks for a standard font.
Fall back to the root path `/` instead, which is absolute on every platform
and resolves the same way as `file:///` elsewhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrowly scoped fix preserves normal module resolution, and existing CommonJS bundling tests cover the affected loading and font-registration behavior.

No open issues 🎉

What changed in this PR

Fixes Windows load failures when PDFKit’s Node build is bundled as CommonJS, following up on #1802.

Changes:

  • Replaces the createRequire fallback URL file:/// with /, avoiding Windows drive-letter validation failures while preserving normal module loading.
  • Updates the comment to explain the platform-specific reason for the fallback.
File Description
lib/​document.node.js Uses a cross-platform fallback for createRequire and documents why it is needed.

Review effort: Balanced

@blikblum
blikblum merged commit 9de53ec into foliojs:master Oct 23, 2026
3 checks passed
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.

3 participants