Repository navigation
Fix a CommonJS bundle of the Node build throwing on load on Windows - #1828
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🟢 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
createRequirefallback URLfile:///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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.urlis undefined andlib/document.node.jsfalls back tocreateRequire('file:///'). That URL has no drive letter, so on WindowscreateRequirethrows while the bundle is loading:Repro on Windows with master packed via
npm pack:The fallback is now
/, which is absolute on every platform. On Linux and macOS it resolves the same wayfile:///did. On Windows the bundle now loads, an unregistered standard font still fails with the "Cannot load the standard font" error, and fonts passed toregisterStdFonts()render.The "as CommonJS" cases in
tests/unit/bundling.spec.jsalready 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:
registerStdFonts, resolve standard fonts and the ICC profile on first use #1802)I used Claude Code to write this change.