fix: preserve runtime createRequire in ESM builds#1334
Open
Jaksenc wants to merge 1 commit into
Open
Conversation
styfle
reviewed
Jun 25, 2026
| try { | ||
| const { code, assets } = await ncc(input, { | ||
| cache: false, | ||
| esm: true, |
Member
There was a problem hiding this comment.
Did you add the programatic api here instead of cli because you want to set esm: true?
If thats the only reason, its probably best to rename the .ts file to .mts or set type:module in package.json instead.
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.
Problem
Since 0.38.2, an ESM build can replace a runtime
createRequire(...)binding withundefined, causing the later runtimerequire(...)call to throw.Change
createRequireparser handling in ncc so Node evaluatesthe call at runtime.
module and executes the emitted
.mjsbundle.This keeps the fix at ncc's Webpack configuration boundary and covers the
reported runtime behavior without changing dependency tracing.
Validation
node scripts/build.js --no-cachenode --expose-gc --max_old_space_size=4096 node_modules/jest/bin/jest.js --runInBand -t 'should preserve runtime createRequire in ESM builds' test/integration.test.jspnpm test -- test/unit.test.jsnode --expose-gc --max_old_space_size=4096 node_modules/jest/bin/jest.js --runInBand test/integration.test.js --testNamePattern='^(?!should execute "ncc run (?:canvas|sharp|socket\.io)\.js"$).*'createRequireparsing restored reproducedthe emitted
undefinedbinding and runtimeTypeError.Risk
The parser option applies to all JavaScript handled by ncc. It intentionally
preserves
createRequirefor Node to evaluate at runtime, so modules selectedonly at runtime must still be deployed with the bundle.
The cross-platform Node 22/24/26 matrix remains for CI. A local CLI watch test
failed with Watchpack
EMFILEerrors and reproduced identically onorigin/main, so it is not attributed to this change.Out of scope
Issue linkage
Fixes #1312