Skip to content

Commit 682ec4f

Browse files
committed
module: handle extensionless entry with explicit type=commonjs
1 parent 27ba1cb commit 682ec4f

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

lib/internal/modules/cjs/loader.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,14 @@ Module._extensions['.js'] = function(module, filename) {
19291929
} else {
19301930
format = 'typescript';
19311931
}
1932+
} else if (path.extname(filename) === '') {
1933+
// Extensionless files skip the .js suffix check above. When type: commonjs
1934+
// is explicit, force commonjs format so ESM syntax surfaces as SyntaxError
1935+
// instead of silently delegating to ESM.
1936+
pkg = packageJsonReader.getNearestParentPackageJSON(filename);
1937+
if (pkg?.data?.type === 'commonjs') {
1938+
format = 'commonjs';
1939+
}
19321940
}
19331941
const { source, format: loadedFormat } = loadSource(module, filename, format);
19341942
// Function require shouldn't be used in ES modules when require(esm) is disabled.

0 commit comments

Comments
 (0)