Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1929,6 +1929,14 @@ Module._extensions['.js'] = function(module, filename) {
} else {
format = 'typescript';
}
} else if (path.extname(filename) === '') {
// Extensionless files skip the .js suffix check above. When type: commonjs
// is explicit, force commonjs format so ESM syntax surfaces as SyntaxError
// 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.

}
}
const { source, format: loadedFormat } = loadSource(module, filename, format);
// Function require shouldn't be used in ES modules when require(esm) is disabled.
Expand Down
50 changes: 50 additions & 0 deletions test/es-module/test-extensionless-esm-type-commonjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict';

const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { spawnSync } = require('node:child_process');

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).

tmpdir.refresh();

const dir = tmpdir.resolve('extensionless-esm-commonjs');
fs.mkdirSync(dir, { recursive: true });

// package.json with type: commonjs
fs.writeFileSync(
path.join(dir, 'package.json'),
'{\n "type": "commonjs"\n}\n',
'utf8'
);

// Extensionless executable with shebang + ESM syntax.
// NOTE: Execute via process.execPath to avoid PATH/env differences.
const scriptPath = path.join(dir, 'script'); // no extension
fs.writeFileSync(
scriptPath,
'#!/usr/bin/env node\n' +
"console.log('script STARTED')\n" +
"import { version } from 'node:process'\n" +
'console.log(version)\n',
'utf8'
);
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.

cwd: dir,
encoding: 'utf8'
});

assert.ifError(r.error);

assert.notEqual(
r.status,
0,
`unexpected exit code 0; stdout=${JSON.stringify(r.stdout)} stderr=${JSON.stringify(r.stderr)}`
);
assert.ok(r.stderr && r.stderr.length > 0, 'expected stderr to contain an error message');
});