diff --git a/lib/internal/loader/ModuleJob.js b/lib/internal/loader/ModuleJob.js index a17c501609a426..8aa1b4b0513500 100644 --- a/lib/internal/loader/ModuleJob.js +++ b/lib/internal/loader/ModuleJob.js @@ -2,6 +2,7 @@ const { ModuleWrap } = internalBinding('module_wrap'); const { SafeSet, SafePromise } = require('internal/safe_globals'); +const { decorateErrorStack } = require('internal/util'); const assert = require('assert'); const resolvedPromise = SafePromise.resolve(); @@ -81,7 +82,12 @@ class ModuleJob { } throw e; } - this.module.instantiate(); + try { + this.module.instantiate(); + } catch (e) { + decorateErrorStack(e); + throw e; + } for (const dependencyJob of jobsInGraph) { // Calling `this.module.instantiate()` instantiates not only the // ModuleWrap in this module, but all modules in the graph. diff --git a/src/module_wrap.cc b/src/module_wrap.cc index 2474b362d7d858..b8970d4fb3222f 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -179,6 +179,7 @@ void ModuleWrap::Link(const FunctionCallbackInfo& args) { } void ModuleWrap::Instantiate(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); Isolate* isolate = args.GetIsolate(); Local that = args.This(); Local context = that->CreationContext(); @@ -186,6 +187,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo& args) { ModuleWrap* obj = Unwrap(that); CHECK_NE(obj, nullptr); Local module = obj->module_.Get(isolate); + TryCatch try_catch(isolate); Maybe ok = module->InstantiateModule(context, ModuleWrap::ResolveCallback); @@ -195,6 +197,12 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo& args) { obj->resolve_cache_.clear(); if (!ok.FromMaybe(false)) { + CHECK(try_catch.HasCaught()); + CHECK(!try_catch.Message().IsEmpty()); + CHECK(!try_catch.Exception().IsEmpty()); + AppendExceptionLine(env, try_catch.Exception(), try_catch.Message(), + ErrorHandlingMode::MODULE_ERROR); + try_catch.ReThrow(); return; } } diff --git a/test/fixtures/es-module-loaders/module-named-exports.mjs b/test/fixtures/es-module-loaders/module-named-exports.mjs new file mode 100644 index 00000000000000..04f7f43ebd8b90 --- /dev/null +++ b/test/fixtures/es-module-loaders/module-named-exports.mjs @@ -0,0 +1,2 @@ +export const foo = 'foo'; +export const bar = 'bar'; diff --git a/test/fixtures/es-module-loaders/syntax-error-import.mjs b/test/fixtures/es-module-loaders/syntax-error-import.mjs new file mode 100644 index 00000000000000..9cad68c7ce2a30 --- /dev/null +++ b/test/fixtures/es-module-loaders/syntax-error-import.mjs @@ -0,0 +1 @@ +import { foo, notfound } from './module-named-exports'; diff --git a/test/message/esm_display_syntax_error_import.mjs b/test/message/esm_display_syntax_error_import.mjs new file mode 100644 index 00000000000000..87cedf1d4edd97 --- /dev/null +++ b/test/message/esm_display_syntax_error_import.mjs @@ -0,0 +1,7 @@ +// Flags: --experimental-modules +/* eslint-disable no-unused-vars */ +import '../common'; +import { + foo, + notfound +} from '../fixtures/es-module-loaders/module-named-exports'; diff --git a/test/message/esm_display_syntax_error_import.out b/test/message/esm_display_syntax_error_import.out new file mode 100644 index 00000000000000..1fb7fdc34a7de4 --- /dev/null +++ b/test/message/esm_display_syntax_error_import.out @@ -0,0 +1,7 @@ +(node:*) ExperimentalWarning: The ESM module loader is experimental. +file:///*/test/message/esm_display_syntax_error_import.mjs:6 + notfound + ^^^^^^^^ +SyntaxError: The requested module does not provide an export named 'notfound' + at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*) + at diff --git a/test/message/esm_display_syntax_error_import_module.mjs b/test/message/esm_display_syntax_error_import_module.mjs new file mode 100644 index 00000000000000..32c0edb3506835 --- /dev/null +++ b/test/message/esm_display_syntax_error_import_module.mjs @@ -0,0 +1,3 @@ +// Flags: --experimental-modules +import '../common'; +import '../fixtures/es-module-loaders/syntax-error-import'; diff --git a/test/message/esm_display_syntax_error_import_module.out b/test/message/esm_display_syntax_error_import_module.out new file mode 100644 index 00000000000000..77fd63891f1ea0 --- /dev/null +++ b/test/message/esm_display_syntax_error_import_module.out @@ -0,0 +1,7 @@ +(node:*) ExperimentalWarning: The ESM module loader is experimental. +file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1 +import { foo, notfound } from './module-named-exports'; + ^^^^^^^^ +SyntaxError: The requested module does not provide an export named 'notfound' + at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*) + at