Skip to content

Commit 94ac44f

Browse files
MylesBorinstargos
authored andcommitted
esm: modify resolution order for specifier flag
Currently `--es-module-specifier-resolution=node` has an alternative resolution order than the default in common.js, this causes inconsistencies. As discussed in @nodejs/modules we want to preserve resolution order between implementations. PR-URL: #29974 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 5616f22 commit 94ac44f

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

lib/internal/modules/esm/default_resolve.js

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const { getOptionValue } = require('internal/options');
99
const preserveSymlinks = getOptionValue('--preserve-symlinks');
1010
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
1111
const experimentalJsonModules = getOptionValue('--experimental-json-modules');
12+
const esModuleSpecifierResolution =
13+
getOptionValue('--es-module-specifier-resolution');
1214
const typeFlag = getOptionValue('--input-type');
1315
const experimentalWasmModules = getOptionValue('--experimental-wasm-modules');
1416
const { resolve: moduleWrapResolve,
@@ -110,6 +112,8 @@ function resolve(specifier, parentURL) {
110112
if (!format) {
111113
if (isMain)
112114
format = type === TYPE_MODULE ? 'module' : 'commonjs';
115+
else if (esModuleSpecifierResolution === 'node')
116+
format = 'commonjs';
113117
else
114118
throw new ERR_UNKNOWN_FILE_EXTENSION(fileURLToPath(url));
115119
}

src/module_wrap.cc

+2-3
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ using v8::Undefined;
4848
using v8::Value;
4949

5050
static const char* const EXTENSIONS[] = {
51-
".mjs",
52-
".cjs",
5351
".js",
5452
".json",
55-
".node"
53+
".node",
54+
".mjs"
5655
};
5756

5857
ModuleWrap::ModuleWrap(Environment* env,

test/es-module/test-esm-specifiers.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Flags: --experimental-modules --es-module-specifier-resolution=node
2-
import { mustNotCall } from '../common';
2+
import { mustNotCall } from '../common/index.mjs';
33
import assert from 'assert';
44

55
// commonJS index.js
@@ -14,8 +14,8 @@ assert.strictEqual(commonjs, 'commonjs');
1414
assert.strictEqual(module, 'module');
1515
assert.strictEqual(success, 'success');
1616
assert.strictEqual(explicit, 'esm');
17-
assert.strictEqual(implicit, 'esm');
18-
assert.strictEqual(implicitModule, 'esm');
17+
assert.strictEqual(implicit, 'cjs');
18+
assert.strictEqual(implicitModule, 'cjs');
1919

2020
async function main() {
2121
try {

test/fixtures/es-module-specifiers/node_modules/implicit-main-type-module/entry.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)