Skip to content

module: fix extensionless entry with explicit type=commonjs#61600

Open
inoway46 wants to merge 7 commits intonodejs:mainfrom
inoway46:fix-extensionless-type-commonjs-esm
Open

module: fix extensionless entry with explicit type=commonjs#61600
inoway46 wants to merge 7 commits intonodejs:mainfrom
inoway46:fix-extensionless-type-commonjs-esm

Conversation

@inoway46
Copy link

@inoway46 inoway46 commented Jan 31, 2026

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 .js suffix check in the CJS loader, so the explicit type: commonjs was 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 type is 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)

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Jan 31, 2026
@inoway46 inoway46 marked this pull request as ready for review January 31, 2026 15:45
Copy link
Member

@joyeecheung joyeecheung left a comment

Choose a reason for hiding this comment

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

Thanks for the PR, a couple of comments, otherwise this looks good.

@inoway46 inoway46 requested a review from joyeecheung February 1, 2026 04:06
@inoway46 inoway46 requested a review from joyeecheung February 1, 2026 17:12

const commonjsDir = fixtures.path('es-modules', 'extensionless-esm-commonjs');

spawnSyncAndAssert(process.execPath, ['--no-experimental-detect-module', './script'], {
Copy link
Member

@joyeecheung joyeecheung Feb 6, 2026

Choose a reason for hiding this comment

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

Hmm, why is --no-experimental-detect-module needed here? I think the original issue is that it fails by default (without this flag)?

Copy link
Member

Choose a reason for hiding this comment

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

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/
})

Copy link
Author

@inoway46 inoway46 Feb 6, 2026

Choose a reason for hiding this comment

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

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.

@inoway46 inoway46 requested a review from joyeecheung February 6, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

With "type": "commonjs", #!/usr/bin/env node file does not execute but returns success and no error messages when the file is ESM

3 participants