Skip to content

Commit 9916458

Browse files
GeoffreyBoothmarco-ippolito
authored andcommittedFeb 11, 2025
module: unflag detect-module
PR-URL: #53619 Backport-PR-URL: #56927 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Refs: #52697
1 parent 0d327c8 commit 9916458

20 files changed

+122
-131
lines changed
 

‎doc/api/cli.md

+16-32
Original file line numberDiff line numberDiff line change
@@ -887,38 +887,6 @@ files with no extension will be treated as WebAssembly if they begin with the
887887
WebAssembly magic number (`\0asm`); otherwise they will be treated as ES module
888888
JavaScript.
889889

890-
### `--experimental-detect-module`
891-
892-
<!-- YAML
893-
added:
894-
- v20.10.0
895-
-->
896-
897-
> Stability: 1.1 - Active development
898-
899-
Node.js will inspect the source code of ambiguous input to determine whether it
900-
contains ES module syntax; if such syntax is detected, the input will be treated
901-
as an ES module.
902-
903-
Ambiguous input is defined as:
904-
905-
* Files with a `.js` extension or no extension; and either no controlling
906-
`package.json` file or one that lacks a `type` field; and
907-
`--experimental-default-type` is not specified.
908-
* String input (`--eval` or STDIN) when neither `--input-type` nor
909-
`--experimental-default-type` are specified.
910-
911-
ES module syntax is defined as syntax that would throw when evaluated as
912-
CommonJS. This includes the following:
913-
914-
* `import` statements (but _not_ `import()` expressions, which are valid in
915-
CommonJS).
916-
* `export` statements.
917-
* `import.meta` references.
918-
* `await` at the top level of a module.
919-
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
920-
`exports`, `__dirname`, `__filename`).
921-
922890
### `--experimental-eventsource`
923891

924892
<!-- YAML
@@ -1540,6 +1508,21 @@ added: v0.8.0
15401508

15411509
Silence deprecation warnings.
15421510

1511+
### `--no-experimental-detect-module`
1512+
1513+
<!-- YAML
1514+
added:
1515+
- v21.1.0
1516+
- v20.10.0
1517+
changes:
1518+
- version:
1519+
- REPLACEME
1520+
pr-url: https://github.com/nodejs/node/pull/53619
1521+
description: Syntax detection is enabled by default.
1522+
-->
1523+
1524+
Disable using [syntax detection][] to determine module type.
1525+
15431526
### `--no-experimental-fetch`
15441527

15451528
<!-- YAML
@@ -3384,6 +3367,7 @@ node --stack-trace-limit=12 -p -e "Error.stackTraceLimit" # prints 12
33843367
[security warning]: #warning-binding-inspector-to-a-public-ipport-combination-is-insecure
33853368
[semi-space]: https://www.memorymanagement.org/glossary/s.html#semi.space
33863369
[single executable application]: single-executable-applications.md
3370+
[syntax detection]: packages.md#syntax-detection
33873371
[test reporters]: test.md#test-reporters
33883372
[timezone IDs]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
33893373
[tracking issue for user-land snapshots]: https://github.com/nodejs/node/issues/44014

‎doc/api/esm.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,7 @@ _isImports_, _conditions_)
10711071
> 10. If _url_ ends in _".js"_, then
10721072
> 1. If _packageType_ is not **null**, then
10731073
> 1. Return _packageType_.
1074-
> 2. If `--experimental-detect-module` is enabled and the result of
1075-
> **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
1074+
> 2. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
10761075
> 1. Return _"module"_.
10771076
> 3. Return _"commonjs"_.
10781077
> 11. If _url_ does not have any extension, then
@@ -1082,8 +1081,7 @@ _isImports_, _conditions_)
10821081
> 1. Return _"wasm"_.
10831082
> 2. If _packageType_ is not **null**, then
10841083
> 1. Return _packageType_.
1085-
> 3. If `--experimental-detect-module` is enabled and the source of
1086-
> module contains static import or export syntax, then
1084+
> 3. If the result of **DETECT\_MODULE\_SYNTAX**(_source_) is true, then
10871085
> 1. Return _"module"_.
10881086
> 4. Return _"commonjs"_.
10891087
> 12. Return **undefined** (will throw during load phase).

‎doc/api/modules.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ loaded by `require()` meets the following requirements:
192192
1. The file has a `.mjs` extension.
193193
2. The file has a `.js` extension, and the closest `package.json` contains `"type": "module"`
194194
3. The file has a `.js` extension, the closest `package.json` does not contain
195-
`"type": "commonjs"`, and `--experimental-detect-module` is enabled.
195+
`"type": "commonjs"`, and the module contains ES module syntax.
196196

197197
`require()` will load the requested module as an ES Module, and return
198198
the module namespace object. In this case it is similar to dynamic
@@ -279,7 +279,7 @@ require(X) from module at path Y
279279
280280
MAYBE_DETECT_AND_LOAD(X)
281281
1. If X parses as a CommonJS module, load X as a CommonJS module. STOP.
282-
2. Else, if `--experimental-require-module` and `--experimental-detect-module` are
282+
2. Else, if `--experimental-require-module` is
283283
enabled, and the source code of X can be parsed as ECMAScript module using
284284
<a href="esm.md#resolver-algorithm-specification">DETECT_MODULE_SYNTAX defined in
285285
the ESM resolver</a>,

‎doc/api/packages.md

+47-9
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ expressions:
6969
* Strings passed in as an argument to `--eval`, or piped to `node` via `STDIN`,
7070
with the flag `--input-type=module`.
7171

72-
* When using [`--experimental-detect-module`][], code containing syntax only
73-
successfully parsed as [ES modules][], such as `import` or `export`
74-
statements or `import.meta`, having no explicit marker of how it should be
75-
interpreted. Explicit markers are `.mjs` or `.cjs` extensions, `package.json`
76-
`"type"` fields with either `"module"` or `"commonjs"` values, or
77-
`--input-type` or `--experimental-default-type` flags. Dynamic `import()`
78-
expressions are supported in either CommonJS or ES modules and would not
79-
cause a file to be treated as an ES module.
72+
* Code containing syntax only successfully parsed as [ES modules][], such as
73+
`import` or `export` statements or `import.meta`, with no explicit marker of
74+
how it should be interpreted. Explicit markers are `.mjs` or `.cjs`
75+
extensions, `package.json` `"type"` fields with either `"module"` or
76+
`"commonjs"` values, or `--input-type` or `--experimental-default-type` flags.
77+
Dynamic `import()` expressions are supported in either CommonJS or ES modules
78+
and would not force a file to be treated as an ES module. See
79+
[Syntax detection][].
8080

8181
Node.js will treat the following as [CommonJS][] when passed to `node` as the
8282
initial input, or when referenced by `import` statements or `import()`
@@ -115,6 +115,44 @@ package in case the default type of Node.js ever changes, and it will also make
115115
things easier for build tools and loaders to determine how the files in the
116116
package should be interpreted.
117117

118+
### Syntax detection
119+
120+
<!-- YAML
121+
added:
122+
- v21.1.0
123+
- v20.10.0
124+
changes:
125+
- version:
126+
- REPLACEME
127+
pr-url: https://github.com/nodejs/node/pull/53619
128+
description: Syntax detection is enabled by default.
129+
-->
130+
131+
> Stability: 1.2 - Release candidate
132+
133+
Node.js will inspect the source code of ambiguous input to determine whether it
134+
contains ES module syntax; if such syntax is detected, the input will be treated
135+
as an ES module.
136+
137+
Ambiguous input is defined as:
138+
139+
* Files with a `.js` extension or no extension; and either no controlling
140+
`package.json` file or one that lacks a `type` field; and
141+
`--experimental-default-type` is not specified.
142+
* String input (`--eval` or STDIN) when neither `--input-type` nor
143+
`--experimental-default-type` are specified.
144+
145+
ES module syntax is defined as syntax that would throw when evaluated as
146+
CommonJS. This includes the following:
147+
148+
* `import` statements (but _not_ `import()` expressions, which are valid in
149+
CommonJS).
150+
* `export` statements.
151+
* `import.meta` references.
152+
* `await` at the top level of a module.
153+
* Lexical redeclarations of the CommonJS wrapper variables (`require`, `module`,
154+
`exports`, `__dirname`, `__filename`).
155+
118156
### Modules loaders
119157

120158
Node.js has two systems for resolving a specifier and loading modules.
@@ -1369,6 +1407,7 @@ This field defines [subpath imports][] for the current package.
13691407
[ES modules]: esm.md
13701408
[Node.js documentation for this section]: https://github.com/nodejs/node/blob/HEAD/doc/api/packages.md#conditions-definitions
13711409
[Runtime Keys]: https://runtime-keys.proposal.wintercg.org/
1410+
[Syntax detection]: #syntax-detection
13721411
[WinterCG]: https://wintercg.org/
13731412
[`"exports"`]: #exports
13741413
[`"imports"`]: #imports
@@ -1378,7 +1417,6 @@ This field defines [subpath imports][] for the current package.
13781417
[`"type"`]: #type
13791418
[`--conditions` / `-C` flag]: #resolving-user-conditions
13801419
[`--experimental-default-type`]: cli.md#--experimental-default-typetype
1381-
[`--experimental-detect-module`]: cli.md#--experimental-detect-module
13821420
[`--no-addons` flag]: cli.md#--no-addons
13831421
[`ERR_PACKAGE_PATH_NOT_EXPORTED`]: errors.md#err_package_path_not_exported
13841422
[`esm`]: https://github.com/standard-things/esm#readme

‎lib/internal/main/check_syntax.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,23 @@ function loadESMIfNeeded(cb) {
5757
}
5858

5959
async function checkSyntax(source, filename) {
60-
let isModule = true;
60+
let format;
6161
if (filename === '[stdin]' || filename === '[eval]') {
62-
isModule = getOptionValue('--input-type') === 'module' ||
63-
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs');
62+
format = (getOptionValue('--input-type') === 'module' ||
63+
(getOptionValue('--experimental-default-type') === 'module' && getOptionValue('--input-type') !== 'commonjs')) ?
64+
'module' : 'commonjs';
6465
} else {
6566
const { defaultResolve } = require('internal/modules/esm/resolve');
6667
const { defaultGetFormat } = require('internal/modules/esm/get_format');
6768
const { url } = await defaultResolve(pathToFileURL(filename).toString());
68-
const format = await defaultGetFormat(new URL(url));
69-
isModule = format === 'module';
69+
format = await defaultGetFormat(new URL(url));
7070
}
7171

72-
if (isModule) {
72+
if (format === 'module') {
7373
const { ModuleWrap } = internalBinding('module_wrap');
7474
new ModuleWrap(filename, undefined, source, 0, 0);
7575
return;
7676
}
7777

78-
wrapSafe(filename, source, undefined, 'commonjs');
78+
wrapSafe(filename, source, undefined, format);
7979
}

‎lib/internal/modules/run_main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function runEntryPointWithESMLoader(callback) {
148148
* by `require('module')`) even when the entry point is ESM.
149149
* This monkey-patchable code is bypassed under `--experimental-default-type=module`.
150150
* Because of backwards compatibility, this function is exposed publicly via `import { runMain } from 'node:module'`.
151-
* When `--experimental-detect-module` is passed, this function will attempt to run ambiguous (no explicit extension, no
151+
* Because of module detection, this function will attempt to run ambiguous (no explicit extension, no
152152
* `package.json` type field) entry points as CommonJS first; under certain conditions, it will retry running as ESM.
153153
* @param {string} main - First positional CLI argument, such as `'entry.js'` from `node entry.js`
154154
*/

‎src/node_options.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
364364
"when ambiguous modules fail to evaluate because they contain "
365365
"ES module syntax, try again to evaluate them as ES modules",
366366
&EnvironmentOptions::detect_module,
367-
kAllowedInEnvvar);
367+
kAllowedInEnvvar,
368+
true);
368369
AddOption("--experimental-print-required-tla",
369370
"Print pending top-level await. If --experimental-require-module "
370371
"is true, evaluate asynchronous graphs loaded by `require()` but "

‎src/node_options.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class EnvironmentOptions : public Options {
115115
public:
116116
bool abort_on_uncaught_exception = false;
117117
std::vector<std::string> conditions;
118-
bool detect_module = false;
118+
bool detect_module = true;
119119
bool print_required_tla = false;
120120
bool require_module = false;
121121
std::string dns_result_order;

‎test/es-module/test-esm-cjs-exports.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ describe('ESM: importing CJS', { concurrency: true }, () => {
2121
const invalidEntry = fixtures.path('/es-modules/cjs-exports-invalid.mjs');
2222
const { code, signal, stderr } = await spawnPromisified(execPath, [invalidEntry]);
2323

24+
assert.match(stderr, /SyntaxError: The requested module '\.\/invalid-cjs\.js' does not provide an export named 'default'/);
2425
assert.strictEqual(code, 1);
2526
assert.strictEqual(signal, null);
26-
assert.ok(stderr.includes('Warning: To load an ES module'));
27-
assert.ok(stderr.includes('Unexpected token \'export\''));
2827
});
2928
});

‎test/es-module/test-esm-detect-ambiguous.mjs

+15-25
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import { spawn } from 'node:child_process';
44
import { describe, it } from 'node:test';
55
import { strictEqual, match } from 'node:assert';
66

7-
describe('--experimental-detect-module', { concurrency: true }, () => {
8-
describe('string input', { concurrency: true }, () => {
7+
describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, () => {
8+
describe('string input', { concurrency: !process.env.TEST_PARALLEL }, () => {
99
it('permits ESM syntax in --eval input without requiring --input-type=module', async () => {
1010
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
11-
'--experimental-detect-module',
1211
'--eval',
1312
'import { version } from "node:process"; console.log(version);',
1413
]);
@@ -22,17 +21,14 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
2221
// ESM is unsupported for --print via --input-type=module
2322

2423
it('permits ESM syntax in STDIN input without requiring --input-type=module', async () => {
25-
const child = spawn(process.execPath, [
26-
'--experimental-detect-module',
27-
]);
24+
const child = spawn(process.execPath, []);
2825
child.stdin.end('console.log(typeof import.meta.resolve)');
2926

3027
match((await child.stdout.toArray()).toString(), /^function\r?\n$/);
3128
});
3229

3330
it('should be overridden by --input-type', async () => {
3431
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
35-
'--experimental-detect-module',
3632
'--input-type=commonjs',
3733
'--eval',
3834
'import.meta.url',
@@ -46,7 +42,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
4642

4743
it('should not switch to module if code is parsable as script', async () => {
4844
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
49-
'--experimental-detect-module',
5045
'--eval',
5146
'let __filename,__dirname,require,module,exports;this.a',
5247
]);
@@ -59,7 +54,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
5954

6055
it('should be overridden by --experimental-default-type', async () => {
6156
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
62-
'--experimental-detect-module',
6357
'--experimental-default-type=commonjs',
6458
'--eval',
6559
'import.meta.url',
@@ -73,7 +67,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
7367

7468
it('does not trigger detection via source code `eval()`', async () => {
7569
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
76-
'--experimental-detect-module',
7770
'--eval',
7871
'eval("import \'nonexistent\';");',
7972
]);
@@ -115,7 +108,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
115108
it(testName, async () => {
116109
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
117110
'--no-warnings',
118-
'--experimental-detect-module',
119111
entryPath,
120112
]);
121113

@@ -157,7 +149,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
157149
it(testName, async () => {
158150
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
159151
'--no-warnings',
160-
'--experimental-detect-module',
161152
entryPath,
162153
]);
163154

@@ -171,7 +162,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
171162
it('should not hint wrong format in resolve hook', async () => {
172163
let writeSync;
173164
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
174-
'--experimental-detect-module',
175165
'--no-warnings',
176166
'--loader',
177167
`data:text/javascript,import { writeSync } from "node:fs"; export ${encodeURIComponent(
@@ -209,7 +199,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
209199
]) {
210200
it(testName, async () => {
211201
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
212-
'--experimental-detect-module',
213202
entryPath,
214203
]);
215204

@@ -238,7 +227,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
238227
]) {
239228
it(testName, async () => {
240229
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
241-
'--experimental-detect-module',
242230
entryPath,
243231
]);
244232

@@ -254,7 +242,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
254242
describe('syntax that errors in CommonJS but works in ESM', { concurrency: true }, () => {
255243
it('permits top-level `await`', async () => {
256244
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
257-
'--experimental-detect-module',
258245
'--eval',
259246
'await Promise.resolve(); console.log("executed");',
260247
]);
@@ -267,7 +254,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
267254

268255
it('permits top-level `await` above import/export syntax', async () => {
269256
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
270-
'--experimental-detect-module',
271257
'--eval',
272258
'await Promise.resolve(); import "node:os"; console.log("executed");',
273259
]);
@@ -280,7 +266,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
280266

281267
it('still throws on `await` in an ordinary sync function', async () => {
282268
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
283-
'--experimental-detect-module',
284269
'--eval',
285270
'function fn() { await Promise.resolve(); } fn();',
286271
]);
@@ -293,7 +278,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
293278

294279
it('throws on undefined `require` when top-level `await` triggers ESM parsing', async () => {
295280
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
296-
'--experimental-detect-module',
297281
'--eval',
298282
'const fs = require("node:fs"); await Promise.resolve();',
299283
]);
@@ -307,7 +291,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
307291
it('permits declaration of CommonJS module variables', async () => {
308292
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
309293
'--no-warnings',
310-
'--experimental-detect-module',
311294
fixtures.path('es-modules/package-without-type/commonjs-wrapper-variables.js'),
312295
]);
313296

@@ -319,7 +302,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
319302

320303
it('permits declaration of CommonJS module variables above import/export', async () => {
321304
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
322-
'--experimental-detect-module',
323305
'--eval',
324306
'const module = 3; import "node:os"; console.log("executed");',
325307
]);
@@ -332,7 +314,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
332314

333315
it('still throws on double `const` declaration not at the top level', async () => {
334316
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
335-
'--experimental-detect-module',
336317
'--eval',
337318
'function fn() { const require = 1; const require = 2; } fn();',
338319
]);
@@ -361,7 +342,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
361342
]) {
362343
it(testName, async () => {
363344
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
364-
'--experimental-detect-module',
365345
entryPath,
366346
]);
367347

@@ -374,7 +354,6 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
374354

375355
it('warns only once for a package.json that affects multiple files', async () => {
376356
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
377-
'--experimental-detect-module',
378357
fixtures.path('es-modules/package-without-type/detected-as-esm.js'),
379358
]);
380359

@@ -384,6 +363,18 @@ describe('--experimental-detect-module', { concurrency: true }, () => {
384363
strictEqual(code, 0);
385364
strictEqual(signal, null);
386365
});
366+
367+
it('can be disabled via --no-experimental-detect-module', async () => {
368+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
369+
'--no-experimental-detect-module',
370+
fixtures.path('es-modules/package-without-type/module.js'),
371+
]);
372+
373+
match(stderr, /SyntaxError: Unexpected token 'export'/);
374+
strictEqual(stdout, '');
375+
strictEqual(code, 1);
376+
strictEqual(signal, null);
377+
});
387378
});
388379
});
389380

@@ -410,7 +401,6 @@ describe('Wrapping a `require` of an ES module while using `--abort-on-uncaught-
410401
describe('when working with Worker threads', () => {
411402
it('should support sloppy scripts that declare CJS "global-like" variables', async () => {
412403
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
413-
'--experimental-detect-module',
414404
'--eval',
415405
'new worker_threads.Worker("let __filename,__dirname,require,module,exports;this.a",{eval:true})',
416406
]);

‎test/es-module/test-esm-extensionless-esm-and-wasm.mjs

+7-14
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,20 @@ describe('extensionless Wasm modules within a "type": "module" package scope', {
5555
});
5656
});
5757

58-
describe('extensionless ES modules within no package scope', { concurrency: true }, () => {
59-
// This succeeds with `--experimental-default-type=module`
60-
it('should error as the entry point', async () => {
58+
describe('extensionless ES modules within no package scope', { concurrency: !process.env.TEST_PARALLEL }, () => {
59+
it('should run as the entry point', async () => {
6160
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
6261
fixtures.path('es-modules/noext-esm'),
6362
]);
6463

65-
match(stderr, /SyntaxError/);
66-
strictEqual(stdout, '');
67-
strictEqual(code, 1);
64+
strictEqual(stdout, 'executed\n');
65+
strictEqual(stderr, '');
66+
strictEqual(code, 0);
6867
strictEqual(signal, null);
6968
});
7069

71-
// This succeeds with `--experimental-default-type=module`
72-
it('should error on import', async () => {
73-
try {
74-
await import(fixtures.fileURL('es-modules/noext-esm'));
75-
mustNotCall();
76-
} catch (err) {
77-
ok(err instanceof SyntaxError);
78-
}
70+
it('should run on import', async () => {
71+
await import(fixtures.fileURL('es-modules/noext-esm'));
7972
});
8073
});
8174

‎test/es-module/test-esm-import-flag.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ describe('import modules using --import', { concurrency: true }, () => {
146146
]
147147
);
148148

149-
assert.match(stderr, /SyntaxError: Unexpected token 'export'/);
149+
assert.strictEqual(stderr, '');
150150
assert.match(stdout, /^\.mjs file\r?\n$/);
151-
assert.strictEqual(code, 1);
151+
assert.strictEqual(code, 0);
152152
assert.strictEqual(signal, null);
153153
});
154154

‎test/es-module/test-esm-loader-hooks.mjs

+8-8
Original file line numberDiff line numberDiff line change
@@ -716,15 +716,15 @@ describe('Loader hooks', { concurrency: true }, () => {
716716
'--no-warnings',
717717
'--experimental-loader',
718718
`data:text/javascript,import{readFile}from"node:fs/promises";import{fileURLToPath}from"node:url";export ${
719-
async function load(u, c, n) {
720-
const r = await n(u, c);
721-
if (u.endsWith('/common/index.js')) {
722-
r.source = '"use strict";module.exports=require("node:module").createRequire(' +
723-
`${JSON.stringify(u)})(${JSON.stringify(fileURLToPath(u))});\n`;
724-
} else if (c.format === 'commonjs') {
725-
r.source = await readFile(new URL(u));
719+
async function load(url, context, nextLoad) {
720+
const result = await nextLoad(url, context);
721+
if (url.endsWith('/common/index.js')) {
722+
result.source = '"use strict";module.exports=require("node:module").createRequire(' +
723+
`${JSON.stringify(url)})(${JSON.stringify(fileURLToPath(url))});\n`;
724+
} else if (url.startsWith('file:') && (context.format == null || context.format === 'commonjs')) {
725+
result.source = await readFile(new URL(url));
726726
}
727-
return r;
727+
return result;
728728
}}`,
729729
'--experimental-loader',
730730
fixtures.fileURL('es-module-loaders/loader-resolve-passthru.mjs'),

‎test/es-module/test-esm-resolve-type.mjs

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ try {
4040
[
4141
[ '/es-modules/package-type-module/index.js', 'module' ],
4242
[ '/es-modules/package-type-commonjs/index.js', 'commonjs' ],
43-
[ '/es-modules/package-without-type/index.js', 'commonjs' ],
44-
[ '/es-modules/package-without-pjson/index.js', 'commonjs' ],
43+
[ '/es-modules/package-without-type/index.js', null ],
44+
[ '/es-modules/package-without-pjson/index.js', null ],
4545
].forEach(([ testScript, expectedType ]) => {
4646
const resolvedPath = path.resolve(fixtures.path(testScript));
4747
const resolveResult = resolve(url.pathToFileURL(resolvedPath));
@@ -54,11 +54,11 @@ try {
5454
*
5555
* for test-module-ne: everything .js that is not 'module' is 'commonjs'
5656
*/
57-
for (const [ moduleName, moduleExtenstion, moduleType, expectedResolvedType ] of
57+
for (const [ moduleName, moduleExtension, moduleType, expectedResolvedType ] of
5858
[ [ 'test-module-mainjs', 'js', 'module', 'module'],
5959
[ 'test-module-mainmjs', 'mjs', 'module', 'module'],
6060
[ 'test-module-cjs', 'js', 'commonjs', 'commonjs'],
61-
[ 'test-module-ne', 'js', undefined, 'commonjs'],
61+
[ 'test-module-ne', 'js', undefined, null],
6262
]) {
6363
process.chdir(previousCwd);
6464
tmpdir.refresh();
@@ -72,14 +72,14 @@ try {
7272
const mDir = rel(`node_modules/${moduleName}`);
7373
const subDir = rel(`node_modules/${moduleName}/subdir`);
7474
const pkg = rel(`node_modules/${moduleName}/package.json`);
75-
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtenstion}`);
75+
const script = rel(`node_modules/${moduleName}/subdir/mainfile.${moduleExtension}`);
7676

7777
createDir(nmDir);
7878
createDir(mDir);
7979
createDir(subDir);
8080
const pkgJsonContent = {
8181
...(moduleType !== undefined) && { type: moduleType },
82-
main: `subdir/mainfile.${moduleExtenstion}`
82+
main: `subdir/mainfile.${moduleExtension}`
8383
};
8484
fs.writeFileSync(pkg, JSON.stringify(pkgJsonContent));
8585
fs.writeFileSync(script,

‎test/es-module/test-require-module-detect-entry-point-aou.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-require-module --experimental-detect-module --abort-on-uncaught-exception
1+
// Flags: --experimental-require-module --abort-on-uncaught-exception
22

33
import { mustCall } from '../common/index.mjs';
44
const fn = mustCall(() => {

‎test/es-module/test-require-module-detect-entry-point.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-require-module --experimental-detect-module
1+
// Flags: --experimental-require-module
22

33
import { mustCall } from '../common/index.mjs';
44
const fn = mustCall(() => {

‎test/es-module/test-require-module-dont-detect-cjs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-require-module --experimental-detect-module
1+
// Flags: --experimental-require-module
22
'use strict';
33

44
require('../common');

‎test/es-module/test-require-module-implicit.js

-12
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,6 @@
66
const common = require('../common');
77
const assert = require('assert');
88

9-
assert.throws(() => {
10-
require('../fixtures/es-modules/package-without-type/noext-esm');
11-
}, {
12-
message: /Unexpected token 'export'/
13-
});
14-
15-
assert.throws(() => {
16-
require('../fixtures/es-modules/loose.js');
17-
}, {
18-
message: /Unexpected token 'export'/
19-
});
20-
219
{
2210
// .mjs should not be matched as default extensions.
2311
const id = '../fixtures/es-modules/should-not-be-resolved';

‎test/es-module/test-require-module-with-detection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Flags: --experimental-require-module --experimental-detect-module
1+
// Flags: --experimental-require-module
22
'use strict';
33

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

‎test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function load(url, context, next) {
2828
source: generateBuiltinModule(urlObj.pathname),
2929
format: 'commonjs',
3030
};
31-
} else if (context.format === 'commonjs') {
31+
} else if (context.format === undefined || context.format === null || context.format === 'commonjs') {
3232
return {
3333
shortCircuit: true,
3434
source: readFileSync(new URL(url)),

0 commit comments

Comments
 (0)
Please sign in to comment.