Skip to content

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.


const tmpdir = require('../common/tmpdir');

test('extensionless entry point with ESM syntax under type=commonjs should not silently exit 0', () => {
Copy link
Member

Choose a reason for hiding this comment

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

There is no need to use node:test if there is only one test - it would unnecessarily clutter the CI output with nested tap (and if more tests are added they should be in separate files to avoid building test monoliths).

);
fs.chmodSync(scriptPath, 0o755);

const r = spawnSync(process.execPath, ['./script'], {
Copy link
Member

@joyeecheung joyeecheung Jan 31, 2026

Choose a reason for hiding this comment

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

Can you use spawnSyncAndAssert from test/common/child_process.js for this? Otherwise when the expectation is not met in the CI there won't be a lot of information about what's going on.

// instead of silently delegating to ESM.
pkg = packageJsonReader.getNearestParentPackageJSON(filename);
if (pkg?.data?.type === 'commonjs') {
format = 'commonjs';
Copy link
Member

Choose a reason for hiding this comment

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

N/B: I think we should also allow passing the explicit "module" type field in package.json here.

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