diff --git a/lib/internal/modules/esm/get_format.js b/lib/internal/modules/esm/get_format.js index cd5c88dce8e021..407cb398bbdb9e 100644 --- a/lib/internal/modules/esm/get_format.js +++ b/lib/internal/modules/esm/get_format.js @@ -112,10 +112,8 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE default: { // The user did not pass `--experimental-default-type`. // `source` is undefined when this is called from `defaultResolve`; // but this gets called again from `defaultLoad`/`defaultLoadSync`. - if (getOptionValue('--experimental-detect-module')) { - const format = source ? - (containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs') : - null; + if (getOptionValue('--experimental-detect-module') && source) { + const format = containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs'; if (format === 'module') { // This module has a .js extension, a package.json with no `type` field, and ESM syntax. // Warn about the missing `type` field so that the user can avoid the performance penalty of detection. @@ -154,8 +152,7 @@ function getFileProtocolModuleFormat(url, context = { __proto__: null }, ignoreE return 'commonjs'; } default: { // The user did not pass `--experimental-default-type`. - if (getOptionValue('--experimental-detect-module')) { - if (!source) { return null; } + if (getOptionValue('--experimental-detect-module') && source) { const format = getFormatOfExtensionlessFile(url); if (format === 'module') { return containsModuleSyntax(`${source}`, fileURLToPath(url), url) ? 'module' : 'commonjs';