Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules: explicitly initialize CJS loader #27313

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/bootstrap/pre_execution.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function prepareMainThreadExecution(expandArgv1 = false) {

initializeDeprecations();
initializeFrozenIntrinsics();
initializeCJSLoader();
initializeESMLoader();
loadPreloadModules();
}
Expand Down Expand Up @@ -336,6 +337,10 @@ function initializePolicy() {
}
}

function initializeCJSLoader() {
require('internal/modules/cjs/loader')._initPaths();
}

function initializeESMLoader() {
const experimentalModules = getOptionValue('--experimental-modules');
const experimentalVMModules = getOptionValue('--experimental-vm-modules');
Expand Down Expand Up @@ -397,5 +402,6 @@ module.exports = {
loadPreloadModules,
setupTraceCategoryState,
setupInspectorHooks,
initializeReport
initializeReport,
initializeCJSLoader
};
17 changes: 7 additions & 10 deletions lib/internal/main/check_syntax.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const {
stripShebang, stripBOM
} = require('internal/modules/cjs/helpers');

const {
_resolveFilename: resolveCJSModuleName,
wrap: wrapCJSModule
} = require('internal/modules/cjs/loader');

// TODO(joyeecheung): not every one of these are necessary
prepareMainThreadExecution(true);

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

// This has to be done after prepareMainThreadExecution because it
// relies on process.execPath
const CJSModule = require('internal/modules/cjs/loader');

// Read the source.
const filename = CJSModule._resolveFilename(process.argv[1]);
const filename = resolveCJSModuleName(process.argv[1]);

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

// This has to be done after prepareMainThreadExecution because it
// relies on process.execPath
const CJSModule = require('internal/modules/cjs/loader');

const { getOptionValue } = require('internal/options');
const experimentalModules = getOptionValue('--experimental-modules');
if (experimentalModules) {
Expand All @@ -76,7 +73,7 @@ function checkSyntax(source, filename) {
// Remove BOM.
source = stripBOM(source);
// Wrap it.
source = CJSModule.wrap(source);
joyeecheung marked this conversation as resolved.
Show resolved Hide resolved
source = wrapCJSModule(source);
// Compile the script, this will throw if it fails.
new vm.Script(source, { displayErrors: true, filename });
}
2 changes: 2 additions & 0 deletions lib/internal/main/worker_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const {
setupWarningHandler,
setupDebugEnv,
initializeDeprecations,
initializeCJSLoader,
initializeESMLoader,
initializeFrozenIntrinsics,
initializeReport,
Expand Down Expand Up @@ -104,6 +105,7 @@ port.on('message', (message) => {
}
initializeDeprecations();
initializeFrozenIntrinsics();
initializeCJSLoader();
initializeESMLoader();
loadPreloadModules();
publicWorker.parentPort = publicPort;
Expand Down
2 changes: 0 additions & 2 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,8 +893,6 @@ Module._preloadModules = function(requests) {
parent.require(requests[n]);
};

Module._initPaths();

// Backwards compatibility
Module.Module = Module;

Expand Down