Skip to content

Commit f225058

Browse files
committed
module: unflag detect-module
1 parent d65b170 commit f225058

File tree

6 files changed

+36
-35
lines changed

6 files changed

+36
-35
lines changed

lib/internal/modules/cjs/loader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1379,7 +1379,7 @@ function wrapSafe(filename, content, cjsModuleInstance, codeCache, format) {
13791379
}
13801380

13811381
const isMain = !!(cjsModuleInstance && cjsModuleInstance[kIsMainSymbol]);
1382-
const shouldDetectModule = (format !== 'commonjs' && getOptionValue('--experimental-detect-module'));
1382+
const shouldDetectModule = (format !== 'commonjs');
13831383
const result = compileFunctionForCJSLoader(content, filename, isMain, shouldDetectModule);
13841384

13851385
// cachedDataRejected is only set for cache coming from SEA.

lib/internal/modules/esm/get_format.js

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ const protocolHandlers = {
3333
'node:'() { return 'builtin'; },
3434
};
3535

36+
/**
37+
* Determine whether the given source contains CommonJS or ES module syntax.
38+
* @param {string | Buffer | undefined} source
39+
* @param {URL} url
40+
*/
41+
function detectModuleFormat(source, url) {
42+
if (!source) { return; }
43+
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
44+
}
45+
3646
/**
3747
* @param {URL} parsed
3848
* @returns {string | null}
@@ -112,26 +122,23 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
112122
default: { // The user did not pass `--experimental-default-type`.
113123
// `source` is undefined when this is called from `defaultResolve`;
114124
// but this gets called again from `defaultLoad`/`defaultLoadSync`.
115-
if (getOptionValue('--experimental-detect-module')) {
116-
const format = source ?
117-
(containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs') :
118-
null;
119-
if (format === 'module') {
120-
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
121-
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
122-
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
123-
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
124-
const warning = `${url} parsed as an ES module because module syntax was detected;` +
125-
` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`;
126-
process.emitWarning(warning, {
127-
code: 'MODULE_TYPELESS_PACKAGE_JSON',
128-
});
129-
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
130-
}
125+
// For ambiguous files (no type field, .js extension) we return
126+
// undefined from `resolve` and re-run the check in `load`.
127+
const format = detectModuleFormat(source, url);
128+
if (format === 'module') {
129+
// This module has a .js extension, a package.json with no `type` field, and ESM syntax.
130+
// Warn about the missing `type` field so that the user can avoid the performance penalty of detection.
131+
typelessPackageJsonFilesWarnedAbout ??= new SafeSet();
132+
if (!typelessPackageJsonFilesWarnedAbout.has(pjsonPath)) {
133+
const warning = `${url} parsed as an ES module because module syntax was detected;` +
134+
` to avoid the performance penalty of syntax detection, add "type": "module" to ${pjsonPath}`;
135+
process.emitWarning(warning, {
136+
code: 'MODULE_TYPELESS_PACKAGE_JSON',
137+
});
138+
typelessPackageJsonFilesWarnedAbout.add(pjsonPath);
131139
}
132-
return format;
133140
}
134-
return 'commonjs';
141+
return format;
135142
}
136143
}
137144
}
@@ -154,15 +161,14 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE
154161
return 'commonjs';
155162
}
156163
default: { // The user did not pass `--experimental-default-type`.
157-
if (getOptionValue('--experimental-detect-module')) {
158-
if (!source) { return null; }
159-
const format = getFormatOfExtensionlessFile(url);
160-
if (format === 'module') {
161-
return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';
162-
}
164+
if (!source) {
165+
return;
166+
}
167+
const format = getFormatOfExtensionlessFile(url);
168+
if (format === 'wasm') {
163169
return format;
164170
}
165-
return 'commonjs';
171+
return detectModuleFormat(source, url);
166172
}
167173
}
168174
}

lib/internal/modules/run_main.js

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

lib/internal/process/execution.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ function evalScript(name, body, breakFirstLine, print, shouldLoadESM = false) {
8181

8282
const baseUrl = pathToFileURL(module.filename).href;
8383

84-
if (getOptionValue('--experimental-detect-module') &&
85-
getOptionValue('--input-type') === '' && getOptionValue('--experimental-default-type') === '' &&
84+
if (getOptionValue('--input-type') === '' && getOptionValue('--experimental-default-type') === '' &&
8685
containsModuleSyntax(body, name, null, 'no CJS variables')) {
8786
return evalModuleEntryPoint(body, print);
8887
}

src/node_options.cc

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,7 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
352352
&EnvironmentOptions::conditions,
353353
kAllowedInEnvvar);
354354
AddAlias("-C", "--conditions");
355-
AddOption("--experimental-detect-module",
356-
"when ambiguous modules fail to evaluate because they contain "
357-
"ES module syntax, try again to evaluate them as ES modules",
358-
&EnvironmentOptions::detect_module,
359-
kAllowedInEnvvar);
355+
AddOption("--experimental-detect-module", "", NoOp{}, kAllowedInEnvvar);
360356
AddOption("--experimental-print-required-tla",
361357
"Print pending top-level await. If --experimental-require-module "
362358
"is true, evaluate asynchronous graphs loaded by `require()` but "

src/node_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class EnvironmentOptions : public Options {
110110
public:
111111
bool abort_on_uncaught_exception = false;
112112
std::vector<std::string> conditions;
113-
bool detect_module = false;
113+
bool detect_module = true;
114114
bool print_required_tla = false;
115115
bool require_module = false;
116116
std::string dns_result_order;

0 commit comments

Comments
 (0)