diff --git a/lib/internal/modules/run_main.js b/lib/internal/modules/run_main.js index 1c41cf189da2e5..507d076d162b0b 100644 --- a/lib/internal/modules/run_main.js +++ b/lib/internal/modules/run_main.js @@ -40,11 +40,11 @@ function shouldUseESMLoader(mainPath) { function runMainESM(mainPath) { const esmLoader = require('internal/process/esm_loader'); const { pathToFileURL } = require('internal/url'); - esmLoader.loadESM((ESMLoader) => { + handleMainPromise(esmLoader.loadESM((ESMLoader) => { const main = path.isAbsolute(mainPath) ? pathToFileURL(mainPath).href : mainPath; - handleMainPromise(ESMLoader.import(main)); - }); + return ESMLoader.import(main); + })); } function handleMainPromise(promise) { diff --git a/lib/internal/process/execution.js b/lib/internal/process/execution.js index 61c29d89b266d3..f8d9e043efda82 100644 --- a/lib/internal/process/execution.js +++ b/lib/internal/process/execution.js @@ -43,15 +43,14 @@ function evalModule(source, print) { throw new ERR_EVAL_ESM_CANNOT_PRINT(); } const { log } = require('internal/console/global'); - const asyncESM = require('internal/process/esm_loader'); + const { loadESM } = require('internal/process/esm_loader'); const { handleMainPromise } = require('internal/modules/run_main'); - handleMainPromise((async () => { - const loader = await asyncESM.ESMLoader; + handleMainPromise(loadESM(async (loader) => { const { result } = await loader.eval(source); if (print) { log(result); } - })()); + })); } function evalScript(name, body, breakFirstLine, print) { diff --git a/test/message/esm_display_syntax_error_import.out b/test/message/esm_display_syntax_error_import.out index 387a63a734b512..fe174d54a5c49f 100644 --- a/test/message/esm_display_syntax_error_import.out +++ b/test/message/esm_display_syntax_error_import.out @@ -5,3 +5,4 @@ SyntaxError: The requested module '../fixtures/es-module-loaders/module-named-ex at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*) at async ModuleJob.run (internal/modules/esm/module_job.js:*:*) at async Loader.import (internal/modules/esm/loader.js:*:*) + at async Object.loadESM (internal/process/esm_loader.js:*:*) diff --git a/test/message/esm_display_syntax_error_import_module.out b/test/message/esm_display_syntax_error_import_module.out index ae8b99d55fef20..d220627bd02654 100644 --- a/test/message/esm_display_syntax_error_import_module.out +++ b/test/message/esm_display_syntax_error_import_module.out @@ -5,3 +5,4 @@ SyntaxError: The requested module './module-named-exports.mjs' does not provide at ModuleJob._instantiate (internal/modules/esm/module_job.js:*:*) at async ModuleJob.run (internal/modules/esm/module_job.js:*:*) at async Loader.import (internal/modules/esm/loader.js:*:*) + at async Object.loadESM (internal/process/esm_loader.js:*:*) diff --git a/test/message/esm_loader_not_found_cjs_hint_bare.out b/test/message/esm_loader_not_found_cjs_hint_bare.out index 10fd7a10dce681..6063709859573b 100644 --- a/test/message/esm_loader_not_found_cjs_hint_bare.out +++ b/test/message/esm_loader_not_found_cjs_hint_bare.out @@ -1,6 +1,6 @@ -internal/modules/run_main.js:* - internalBinding('errors').triggerUncaughtException( - ^ +internal/process/esm_loader.js:* + internalBinding('errors').triggerUncaughtException( + ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find module '*test*fixtures*node_modules*some_module*obj' imported from *test*fixtures*esm_loader_not_found_cjs_hint_bare.mjs Did you mean to import some_module/obj.js?