fix: preserve runtime createRequire in ESM builds#1334
Conversation
| try { | ||
| const { code, assets } = await ncc(input, { | ||
| cache: false, | ||
| esm: true, |
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.
There was a problem hiding this comment.
Ohh yeah, that's exactly why I used the programmatic API there. I wanted to force esm: true, but you're right that it made the test more custom than it needed to be.
I moved it back into the normal integration path with a .mjs entry, and kept the actual createRequire bit in a tiny TS file that it imports.
I tried .mts too, but it still goes through the CJS path here, and typed .mts input doesn't seem to hit the TS loader. So .mjs felt like the smallest way to exercise the ESM path without adding .mts support in this PR.
Does that work better?
f24f444 to
e26fc50
Compare
|
🎉 This PR is included in version 0.44.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
.mjsentry that imports theTypeScript runtime payload and loads a runtime-selected JSON module.
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 test/integration.test.js --runInBand --testNamePattern create-require-runtimepnpm 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 (?:axios\.js|canvas\.js|isomorphic-unfetch\.js|request-ts\.ts|request\.js|sharp\.js|socket\.io\.js)"$).*'createRequireparsing restored reproducedTypeError: input_require is not a function.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