diff --git a/lib/internal/modules/esm/default_resolve.js b/lib/internal/modules/esm/default_resolve.js index 7686e3e003eede..88af3cb5f8d286 100644 --- a/lib/internal/modules/esm/default_resolve.js +++ b/lib/internal/modules/esm/default_resolve.js @@ -9,6 +9,8 @@ const { getOptionValue } = require('internal/options'); const preserveSymlinks = getOptionValue('--preserve-symlinks'); const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main'); const experimentalJsonModules = getOptionValue('--experimental-json-modules'); +const esModuleSpecifierResolution = + getOptionValue('--es-module-specifier-resolution'); const typeFlag = getOptionValue('--input-type'); const experimentalWasmModules = getOptionValue('--experimental-wasm-modules'); const { resolve: moduleWrapResolve, @@ -110,6 +112,8 @@ function resolve(specifier, parentURL) { if (!format) { if (isMain) format = type === TYPE_MODULE ? 'module' : 'commonjs'; + else if (esModuleSpecifierResolution === 'node') + format = 'commonjs'; else throw new ERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url)); } diff --git a/src/module_wrap.cc b/src/module_wrap.cc index c2ffee14b23a53..6a97d61d87ee3a 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -48,11 +48,10 @@ using v8::Undefined; using v8::Value; static const char* const EXTENSIONS[] = { - ".mjs", - ".cjs", ".js", ".json", - ".node" + ".node", + ".mjs" }; ModuleWrap::ModuleWrap(Environment* env, diff --git a/test/es-module/test-esm-specifiers.mjs b/test/es-module/test-esm-specifiers.mjs index 0c5e1ac04ad1a8..59d54cbf63dc79 100644 --- a/test/es-module/test-esm-specifiers.mjs +++ b/test/es-module/test-esm-specifiers.mjs @@ -1,5 +1,5 @@ // Flags: --experimental-modules --es-module-specifier-resolution=node -import { mustNotCall } from '../common'; +import { mustNotCall } from '../common/index.mjs'; import assert from 'assert'; // commonJS index.js @@ -14,8 +14,8 @@ assert.strictEqual(commonjs, 'commonjs'); assert.strictEqual(module, 'module'); assert.strictEqual(success, 'success'); assert.strictEqual(explicit, 'esm'); -assert.strictEqual(implicit, 'esm'); -assert.strictEqual(implicitModule, 'esm'); +assert.strictEqual(implicit, 'cjs'); +assert.strictEqual(implicitModule, 'cjs'); async function main() { try { diff --git a/test/fixtures/es-module-specifiers/node_modules/implicit-main-type-module/entry.js b/test/fixtures/es-module-specifiers/node_modules/implicit-main-type-module/entry.js index 5d7af588fd65b7..06db4db1581b44 100644 --- a/test/fixtures/es-module-specifiers/node_modules/implicit-main-type-module/entry.js +++ b/test/fixtures/es-module-specifiers/node_modules/implicit-main-type-module/entry.js @@ -1 +1 @@ -export default 'nope'; +export default 'cjs';