Skip to content

Commit c6b2f56

Browse files
joyeecheungtargos
authored andcommitted
lib: lazy-load deps in modules/run_main.js
So that the file can be snapshotted PR-URL: #45849 Backport-PR-URL: #46425 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent baa280a commit c6b2f56

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

lib/internal/modules/run_main.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ const {
44
ObjectCreate,
55
StringPrototypeEndsWith,
66
} = primordials;
7-
const CJSLoader = require('internal/modules/cjs/loader');
8-
const { Module, toRealPath, readPackageScope } = CJSLoader;
7+
98
const { getOptionValue } = require('internal/options');
109
const path = require('path');
1110

1211
function resolveMainPath(main) {
1312
// Note extension resolution for the main entry point can be deprecated in a
1413
// future major.
1514
// Module._findPath is monkey-patchable here.
15+
const { Module, toRealPath } = require('internal/modules/cjs/loader');
1616
let mainPath = Module._findPath(path.resolve(main), null, true);
1717
if (!mainPath)
1818
return;
@@ -41,6 +41,7 @@ function shouldUseESMLoader(mainPath) {
4141
getOptionValue('--experimental-specifier-resolution');
4242
if (esModuleSpecifierResolution === 'node')
4343
return true;
44+
const { readPackageScope } = require('internal/modules/cjs/loader');
4445
// Determine the module format of the main
4546
if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs'))
4647
return true;
@@ -83,6 +84,7 @@ function executeUserEntryPoint(main = process.argv[1]) {
8384
runMainESM(resolvedMain || main);
8485
} else {
8586
// Module._load is the monkey-patchable CJS module loader.
87+
const { Module } = require('internal/modules/cjs/loader');
8688
Module._load(main, null, true);
8789
}
8890
}

test/fixtures/errors/force_colors.snapshot

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Error: Should include grayed stack trace
88
 at Module._extensions..js (node:internal*modules*cjs*loader:1310:10)
99
 at Module.load (node:internal*modules*cjs*loader:1119:32)
1010
 at Module._load (node:internal*modules*cjs*loader:960:12)
11-
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:86:12)
11+
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:88:12)
1212
 at node:internal*main*run_main_module:23:47
1313

1414
Node.js *

0 commit comments

Comments
 (0)