Skip to content

Commit 7c816b7

Browse files
committed
module: explicitly initialize CJS loader
Explicitly initialize the CJS loader with `module._initPaths()` instead of making it a side-effect of requiring `internal/modules/cjs/loader` - that makes it harder to reason about when it's safe to load `internal/modules/cjs/loader`. PR-URL: #27313 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 528d100 commit 7c816b7

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

lib/internal/bootstrap/pre_execution.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ function prepareMainThreadExecution(expandArgv1 = false) {
4848

4949
initializeDeprecations();
5050
initializeFrozenIntrinsics();
51+
initializeCJSLoader();
5152
initializeESMLoader();
5253
loadPreloadModules();
5354
}
@@ -336,6 +337,10 @@ function initializePolicy() {
336337
}
337338
}
338339

340+
function initializeCJSLoader() {
341+
require('internal/modules/cjs/loader')._initPaths();
342+
}
343+
339344
function initializeESMLoader() {
340345
const experimentalModules = getOptionValue('--experimental-modules');
341346
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
@@ -397,5 +402,6 @@ module.exports = {
397402
loadPreloadModules,
398403
setupTraceCategoryState,
399404
setupInspectorHooks,
400-
initializeReport
405+
initializeReport,
406+
initializeCJSLoader
401407
};

lib/internal/main/check_syntax.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const {
1818
stripShebang, stripBOM
1919
} = require('internal/modules/cjs/helpers');
2020

21+
const {
22+
_resolveFilename: resolveCJSModuleName,
23+
wrap: wrapCJSModule
24+
} = require('internal/modules/cjs/loader');
25+
2126
// TODO(joyeecheung): not every one of these are necessary
2227
prepareMainThreadExecution(true);
2328

@@ -26,12 +31,8 @@ if (process.argv[1] && process.argv[1] !== '-') {
2631
const path = require('path');
2732
process.argv[1] = path.resolve(process.argv[1]);
2833

29-
// This has to be done after prepareMainThreadExecution because it
30-
// relies on process.execPath
31-
const CJSModule = require('internal/modules/cjs/loader');
32-
3334
// Read the source.
34-
const filename = CJSModule._resolveFilename(process.argv[1]);
35+
const filename = resolveCJSModuleName(process.argv[1]);
3536

3637
const fs = require('fs');
3738
const source = fs.readFileSync(filename, 'utf-8');
@@ -51,10 +52,6 @@ function checkSyntax(source, filename) {
5152
// Remove Shebang.
5253
source = stripShebang(source);
5354

54-
// This has to be done after prepareMainThreadExecution because it
55-
// relies on process.execPath
56-
const CJSModule = require('internal/modules/cjs/loader');
57-
5855
const { getOptionValue } = require('internal/options');
5956
const experimentalModules = getOptionValue('--experimental-modules');
6057
if (experimentalModules) {
@@ -76,7 +73,7 @@ function checkSyntax(source, filename) {
7673
// Remove BOM.
7774
source = stripBOM(source);
7875
// Wrap it.
79-
source = CJSModule.wrap(source);
76+
source = wrapCJSModule(source);
8077
// Compile the script, this will throw if it fails.
8178
new vm.Script(source, { displayErrors: true, filename });
8279
}

lib/internal/main/worker_thread.js

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
setupWarningHandler,
1313
setupDebugEnv,
1414
initializeDeprecations,
15+
initializeCJSLoader,
1516
initializeESMLoader,
1617
initializeFrozenIntrinsics,
1718
initializeReport,
@@ -104,6 +105,7 @@ port.on('message', (message) => {
104105
}
105106
initializeDeprecations();
106107
initializeFrozenIntrinsics();
108+
initializeCJSLoader();
107109
initializeESMLoader();
108110
loadPreloadModules();
109111
publicWorker.parentPort = publicPort;

lib/internal/modules/cjs/loader.js

-2
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,6 @@ Module._preloadModules = function(requests) {
893893
parent.require(requests[n]);
894894
};
895895

896-
Module._initPaths();
897-
898896
// Backwards compatibility
899897
Module.Module = Module;
900898

0 commit comments

Comments
 (0)