Skip to content

Commit ea87eea

Browse files
authored
module: fix extensionless CJS files in "type": "module" packages
24b8585 made the CJS loader respect the `package.json` type field for extensionless files, which fixed #61104 but also enforced `"type": "module"` for extensionless files. This broke CJS extensionless files inside ESM packages (e.g. yargs v17). Only enforce `"type": "commonjs"` for extensionless files. For `"type": "module"`, leave format undefined so syntax detection handles it. This restores the documented exception in the CJS documentation that has existed since v16: extensionless files in `"type": "module"` packages are recognized as CJS when included via `require()`. Fixes: #61971 Refs: yargs/yargs#2509 Refs: #61600 PR-URL: #62083 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
1 parent f91cf7f commit ea87eea

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

β€Žlib/internal/modules/cjs/loader.jsβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,12 +1952,12 @@ Module._extensions['.js'] = function(module, filename) {
19521952
format = 'typescript';
19531953
}
19541954
} else if (path.extname(filename) === '') {
1955-
// Extensionless files skip the .js suffix check above. When type is explicit,
1956-
// follow it so ESM syntax surfaces as SyntaxError for commonjs instead of
1957-
// silently delegating to ESM.
1955+
// Extensionless files skip the .js suffix check above. When type is commonjs, follow it so ESM
1956+
// syntax surfaces as SyntaxError. For type: module, leave format undefined so our syntax
1957+
// detection handles it (allowing CJS extensionless files in ESM packages).
19581958
pkg = packageJsonReader.getNearestParentPackageJSON(filename);
19591959
const typeFromPjson = pkg?.data?.type;
1960-
if (typeFromPjson === 'commonjs' || typeFromPjson === 'module') {
1960+
if (typeFromPjson === 'commonjs') {
19611961
format = typeFromPjson;
19621962
}
19631963
}

β€Žtest/es-module/test-extensionless-esm-type-commonjs.jsβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,13 @@ spawnSyncAndAssert(process.execPath, [
1717
stdout: /script STARTED[\s\S]*v\d+\./,
1818
trim: true,
1919
});
20+
21+
// CJS extensionless file inside a type: "module" package should work
22+
// when require()'d. Regression test for https://github.com/nodejs/node/issues/61971
23+
spawnSyncAndAssert(process.execPath, [
24+
'-e', `const m = require(${JSON.stringify(
25+
fixtures.path('es-modules', 'extensionless-cjs-module', 'index')
26+
)}); if (m.hello !== 'world') throw new Error('expected CJS exports, got: ' + JSON.stringify(m))`,
27+
], {
28+
status: 0,
29+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { hello: 'world' };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "module"
3+
}

0 commit comments

Comments
Β (0)