summaryrefslogtreecommitdiff
path: root/lib/internal/modules/cjs/loader.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/internal/modules/cjs/loader.js')
-rw-r--r--lib/internal/modules/cjs/loader.js66
1 files changed, 36 insertions, 30 deletions
diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js
index a8dd3bbaa3..a28383690a 100644
--- a/lib/internal/modules/cjs/loader.js
+++ b/lib/internal/modules/cjs/loader.js
@@ -69,9 +69,14 @@ const {
ERR_REQUIRE_ESM
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
+const {
+ resolveMainPath,
+ shouldUseESMLoader,
+ runMainESM
+} = require('internal/bootstrap/pre_execution');
const pendingDeprecation = getOptionValue('--pending-deprecation');
-module.exports = { wrapSafe, Module };
+module.exports = { wrapSafe, Module, toRealPath, readPackageScope };
let asyncESM, ModuleJob, ModuleWrap, kInstantiated;
@@ -810,6 +815,10 @@ Module.prototype.load = function(filename) {
this.paths = Module._nodeModulePaths(path.dirname(filename));
const extension = findLongestRegisteredExtension(filename);
+ // allow .mjs to be overridden
+ if (filename.endsWith('.mjs') && !Module._extensions['.mjs']) {
+ throw new ERR_REQUIRE_ESM(filename);
+ }
Module._extensions[extension](this, filename);
this.loaded = true;
@@ -823,14 +832,19 @@ Module.prototype.load = function(filename) {
if (module !== undefined && module.module !== undefined) {
if (module.module.getStatus() >= kInstantiated)
module.module.setExport('default', exports);
- } else { // preemptively cache
+ } else {
+ // Preemptively cache
+ // We use a function to defer promise creation for async hooks.
ESMLoader.moduleMap.set(
url,
- new ModuleJob(ESMLoader, url, () =>
+ // Module job creation will start promises.
+ // We make it a function to lazily trigger those promises
+ // for async hooks compatibility.
+ () => new ModuleJob(ESMLoader, url, () =>
new ModuleWrap(url, undefined, ['default'], function() {
this.setExport('default', exports);
})
- )
+ , false /* isMain */, false /* inspectBrk */)
);
}
}
@@ -859,7 +873,7 @@ Module.prototype.require = function(id) {
var resolvedArgv;
let hasPausedEntry = false;
-function wrapSafe(filename, content) {
+function wrapSafe(filename, content, cjsModuleInstance) {
if (patched) {
const wrapper = Module.wrap(content);
return vm.runInThisContext(wrapper, {
@@ -867,7 +881,7 @@ function wrapSafe(filename, content) {
lineOffset: 0,
displayErrors: true,
importModuleDynamically: experimentalModules ? async (specifier) => {
- const loader = await asyncESM.loaderPromise;
+ const loader = asyncESM.ESMLoader;
return loader.import(specifier, normalizeReferrerURL(filename));
} : undefined,
});
@@ -892,9 +906,8 @@ function wrapSafe(filename, content) {
]
);
} catch (err) {
- if (experimentalModules) {
+ if (experimentalModules && process.mainModule === cjsModuleInstance)
enrichCJSError(err);
- }
throw err;
}
@@ -902,7 +915,7 @@ function wrapSafe(filename, content) {
const { callbackMap } = internalBinding('module_wrap');
callbackMap.set(compiled.cacheKey, {
importModuleDynamically: async (specifier) => {
- const loader = await asyncESM.loaderPromise;
+ const loader = asyncESM.ESMLoader;
return loader.import(specifier, normalizeReferrerURL(filename));
}
});
@@ -925,7 +938,7 @@ Module.prototype._compile = function(content, filename) {
}
maybeCacheSourceMap(filename, content, this);
- const compiledWrapper = wrapSafe(filename, content);
+ const compiledWrapper = wrapSafe(filename, content, this);
var inspectorWrapper = null;
if (getOptionValue('--inspect-brk') && process._eval == null) {
@@ -981,7 +994,11 @@ Module._extensions['.js'] = function(module, filename) {
'files in that package scope as ES modules.\nInstead rename ' +
`${basename} to end in .cjs, change the requiring code to use ` +
'import(), or remove "type": "module" from ' +
- `${path.resolve(pkg.path, 'package.json')}.`
+ `${path.resolve(pkg.path, 'package.json')}.`,
+ undefined,
+ undefined,
+ undefined,
+ true
);
warnRequireESM = false;
}
@@ -1024,26 +1041,15 @@ Module._extensions['.node'] = function(module, filename) {
return process.dlopen(module, path.toNamespacedPath(filename));
};
-Module._extensions['.mjs'] = function(module, filename) {
- throw new ERR_REQUIRE_ESM(filename);
-};
-
// Bootstrap main module.
-Module.runMain = function() {
- // Load the main module--the command line argument.
- if (experimentalModules) {
- asyncESM.loaderPromise.then((loader) => {
- return loader.import(pathToFileURL(process.argv[1]).href);
- })
- .catch((e) => {
- internalBinding('errors').triggerUncaughtException(
- e,
- true /* fromPromise */
- );
- });
- return;
+Module.runMain = function(main = process.argv[1]) {
+ const resolvedMain = resolveMainPath(main);
+ const useESMLoader = shouldUseESMLoader(resolvedMain);
+ if (useESMLoader) {
+ runMainESM(resolvedMain || main);
+ } else {
+ Module._load(main, null, true);
}
- Module._load(process.argv[1], null, true);
};
function createRequireFromPath(filename) {
@@ -1164,7 +1170,7 @@ Module.Module = Module;
// We have to load the esm things after module.exports!
if (experimentalModules) {
- asyncESM = require('internal/process/esm_loader');
ModuleJob = require('internal/modules/esm/module_job');
+ asyncESM = require('internal/process/esm_loader');
({ ModuleWrap, kInstantiated } = internalBinding('module_wrap'));
}