GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: fix extensionless CJS files in `"type": "module"` packages · nodejs/node@ea87eea

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules/cjs
    • loader.js
  • test
    • es-module
      • test-extensionless-esm-type-commonjs.js
    • fixtures/es-modules/extensionless-cjs-module
      • index
      • package.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/modules/cjs/loader.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+4-4Lines 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
}
Collapse file

‎test/es-module/test-extensionless-esm-type-commonjs.js‎

Copy file name to clipboardExpand all lines: test/es-module/test-extensionless-esm-type-commonjs.js
+10Lines 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+
});
Collapse file

‎test/fixtures/es-modules/extensionless-cjs-module/index‎

Copy file name to clipboard
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = { hello: 'world' };
Collapse file

‎test/fixtures/es-modules/extensionless-cjs-module/package.json‎

Copy file name to clipboard
+3Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.