Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6c6aefe

Browse files
committedOct 1, 2023
Streamline
1 parent 9688f53 commit 6c6aefe

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed
 

‎lib/internal/modules/run_main.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,15 @@ const path = require('path');
99

1010
/**
1111
* Get the absolute path to the main entry point.
12+
* Only used in `--experimental-default-type=commonjs` mode.
1213
* @param {string} main - Entry point path
1314
*/
1415
function resolveMainPath(main) {
15-
/** @type {string} */
16-
let mainPath;
17-
if (getOptionValue('--experimental-default-type') === 'module') {
18-
mainPath = path.resolve(main);
19-
} else {
20-
// Extension searching for the main entry point is supported only in legacy mode.
21-
// Module._findPath is monkey-patchable here.
22-
const { Module } = require('internal/modules/cjs/loader');
23-
mainPath = Module._findPath(path.resolve(main), null, true);
24-
}
16+
// Note extension resolution for the main entry point can be deprecated in a
17+
// future major.
18+
// Module._findPath is monkey-patchable here.
19+
const { Module } = require('internal/modules/cjs/loader');
20+
let mainPath = Module._findPath(path.resolve(main), null, true);
2521
if (!mainPath) { return; }
2622

2723
const preserveSymlinksMain = getOptionValue('--preserve-symlinks-main');
@@ -35,11 +31,10 @@ function resolveMainPath(main) {
3531

3632
/**
3733
* Determine whether the main entry point should be loaded through the ESM Loader.
34+
* Only used in `--experimental-default-type=commonjs` mode.
3835
* @param {string} mainPath - Absolute path to the main entry point
3936
*/
4037
function shouldUseESMLoader(mainPath) {
41-
if (getOptionValue('--experimental-default-type') === 'module') { return true; }
42-
4338
/**
4439
* @type {string[]} userLoaders A list of custom loaders registered by the user
4540
* (or an empty list when none have been registered).
@@ -64,7 +59,7 @@ function shouldUseESMLoader(mainPath) {
6459

6560
/**
6661
* Run the main entry point through the ESM Loader.
67-
* @param {string} mainPath - Absolute path for the main entry point
62+
* @param {string} mainPath - Path to the main entry point, either absolute or relative to CWD
6863
*/
6964
function runMainESM(mainPath) {
7065
const { loadESM } = require('internal/process/esm_loader');
@@ -102,6 +97,11 @@ async function handleMainPromise(promise) {
10297
* @param {string} main - Resolved absolute path for the main entry point, if found
10398
*/
10499
function executeUserEntryPoint(main = process.argv[1]) {
100+
if (getOptionValue('--experimental-default-type') === 'module') {
101+
runMainESM(main);
102+
return;
103+
}
104+
105105
const resolvedMain = resolveMainPath(main);
106106
const useESMLoader = shouldUseESMLoader(resolvedMain);
107107
if (useESMLoader) {

‎test/es-module/test-esm-type-flag-errors.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('--experimental-default-type=module should not support extension search
2626
cwd: fixtures.path('es-modules/package-without-type'),
2727
});
2828

29-
match(stderr, /ENOENT/);
29+
match(stderr, /ERR_MODULE_NOT_FOUND/);
3030
strictEqual(stdout, '');
3131
strictEqual(code, 1);
3232
strictEqual(signal, null);

0 commit comments

Comments
 (0)
Please sign in to comment.