Skip to content

Commit bd82adb

Browse files
targosMylesBorins
authored andcommitted
esm: exit the process with an error if loader has an issue
Previously, this would trigger an unhandled rejection that the user cannot handle. Fixes: #30205 PR-URL: #30219 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent 7e1f050 commit bd82adb

5 files changed

+43
-10
lines changed

lib/internal/bootstrap/pre_execution.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,16 @@ function runMainESM(mainPath) {
480480
return esmLoader.initializeLoader().then(() => {
481481
const main = path.isAbsolute(mainPath) ?
482482
pathToFileURL(mainPath).href : mainPath;
483-
return esmLoader.ESMLoader.import(main).catch((e) => {
484-
if (hasUncaughtExceptionCaptureCallback()) {
485-
process._fatalException(e);
486-
return;
487-
}
488-
internalBinding('errors').triggerUncaughtException(
489-
e,
490-
true /* fromPromise */
491-
);
492-
});
483+
return esmLoader.ESMLoader.import(main);
484+
}).catch((e) => {
485+
if (hasUncaughtExceptionCaptureCallback()) {
486+
process._fatalException(e);
487+
return;
488+
}
489+
internalBinding('errors').triggerUncaughtException(
490+
e,
491+
true /* fromPromise */
492+
);
493493
});
494494
}
495495

test/message/esm_loader_not_found.mjs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Flags: --experimental-modules --experimental-loader i-dont-exist
2+
import '../common/index.mjs';
3+
console.log('This should not be printed');

test/message/esm_loader_not_found.out

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
3+
internal/modules/esm/default_resolve.js:*
4+
let url = moduleWrapResolve(specifier, parentURL);
5+
^
6+
7+
Error: Cannot find package 'i-dont-exist' imported from *
8+
at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:*:*)
9+
at Loader.resolve (internal/modules/esm/loader.js:*:*)
10+
at Loader.getModuleJob (internal/modules/esm/loader.js:*:*)
11+
at Loader.import (internal/modules/esm/loader.js:*:*)
12+
at internal/process/esm_loader.js:*:*
13+
at Object.initializeLoader (internal/process/esm_loader.js:*:*)
14+
at runMainESM (internal/bootstrap/pre_execution.js:*:*)
15+
at Function.Module.runMain (internal/modules/cjs/loader.js:*:*)
16+
at internal/main/run_main_module.js:*:* {
17+
code: 'ERR_MODULE_NOT_FOUND'
18+
}
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Flags: --experimental-modules --experimental-loader ./test/fixtures/es-module-loaders/syntax-error.mjs
2+
import '../common/index.mjs';
3+
console.log('This should not be printed');
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
3+
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
4+
await async () => 0;
5+
^^^^^
6+
7+
SyntaxError: Unexpected reserved word
8+
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)
9+
at async link (internal/modules/esm/module_job.js:*:*)

0 commit comments

Comments
 (0)