module: fix extensionless entry with explicit type=commonjs#61600
module: fix extensionless entry with explicit type=commonjs#61600inoway46 wants to merge 7 commits intonodejs:mainfrom
Conversation
|
Review requested:
|
joyeecheung
left a comment
There was a problem hiding this comment.
Thanks for the PR, a couple of comments, otherwise this looks good.
|
|
||
| const commonjsDir = fixtures.path('es-modules', 'extensionless-esm-commonjs'); | ||
|
|
||
| spawnSyncAndAssert(process.execPath, ['--no-experimental-detect-module', './script'], { |
There was a problem hiding this comment.
Hmm, why is --no-experimental-detect-module needed here? I think the original issue is that it fails by default (without this flag)?
There was a problem hiding this comment.
As far as I can tell the test should be like this:
spawnSyncAndAssert(process.execPath, [fixtures.path('es-modules', 'extensionless-esm-commonjs', 'script'), {
status: 1,
stderr: /SyntaxError: Cannot use import statement outside a module/
})There was a problem hiding this comment.
Thanks for the review.
I initially thought --no-experimental-detect-module was needed because I accidentally ran the test with a stale local build from another branch. That build did not include the extensionless entry-point handling change, so the extensionless script under type: "commonjs" was still being treated via ESM syntax detection.
After rebuilding on the correct branch (with the fix applied), I confirmed the flag is not necessary. The extensionless script is handled as CommonJS and the expected syntax error is produced. I also updated the test to the inline style you suggested and verified it passes.
Appreciate the guidance.
When an extensionless entry point contains ESM syntax but is in a package with "type": "commonjs" in package.json, the module would silently exit with code 0 without executing or showing any error. This happened because extensionless files skip the
.jssuffix check in the CJS loader, so the explicittype: commonjswas not being enforced, allowing ESM syntax to be silently delegated to ESM loading which never completed before the process exited.This change ensures the CJS loader treats extensionless entry points as commonjs when
typeis explicitly set to "commonjs" in package.json, forcing ESM syntax to surface as a SyntaxError instead of silently exiting.Fixes: #61104
Related: #61171 (alternative approach)