Skip to content

Commit

Permalink
lib: lazy-load deps in modules/run_main.js
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
joyeecheung authored and targos committed Nov 10, 2023
1 parent baa280a commit c6b2f56
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/internal/modules/run_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const {
ObjectCreate,
StringPrototypeEndsWith,
} = primordials;
const CJSLoader = require('internal/modules/cjs/loader');
const { Module, toRealPath, readPackageScope } = CJSLoader;

const { getOptionValue } = require('internal/options');
const path = require('path');

function resolveMainPath(main) {
// Note extension resolution for the main entry point can be deprecated in a
// future major.
// Module._findPath is monkey-patchable here.
const { Module, toRealPath } = require('internal/modules/cjs/loader');
let mainPath = Module._findPath(path.resolve(main), null, true);
if (!mainPath)
return;
Expand Down Expand Up @@ -41,6 +41,7 @@ function shouldUseESMLoader(mainPath) {
getOptionValue('--experimental-specifier-resolution');
if (esModuleSpecifierResolution === 'node')
return true;
const { readPackageScope } = require('internal/modules/cjs/loader');
// Determine the module format of the main
if (mainPath && StringPrototypeEndsWith(mainPath, '.mjs'))
return true;
Expand Down Expand Up @@ -83,6 +84,7 @@ function executeUserEntryPoint(main = process.argv[1]) {
runMainESM(resolvedMain || main);
} else {
// Module._load is the monkey-patchable CJS module loader.
const { Module } = require('internal/modules/cjs/loader');
Module._load(main, null, true);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/errors/force_colors.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Error: Should include grayed stack trace
 at Module._extensions..js (node:internal*modules*cjs*loader:1310:10)
 at Module.load (node:internal*modules*cjs*loader:1119:32)
 at Module._load (node:internal*modules*cjs*loader:960:12)
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:86:12)
 at Function.executeUserEntryPoint [as runMain] (node:internal*modules*run_main:88:12)
 at node:internal*main*run_main_module:23:47

Node.js *

0 comments on commit c6b2f56

Please sign in to comment.