Skip to content

Commit 3b2d8cb

Browse files
targosMylesBorins
authored andcommitted
module: print better message on esm import error
Use the same approach as a previous PR to include the offending line in the output and underline imports of inexistent exports. PR-URL: #17786 Fixes: #17785 Refs: #17281 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 14eb97e commit 3b2d8cb

8 files changed

+42
-1
lines changed

lib/internal/loader/ModuleJob.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const { ModuleWrap } = internalBinding('module_wrap');
44
const { SafeSet, SafePromise } = require('internal/safe_globals');
5+
const { decorateErrorStack } = require('internal/util');
56
const assert = require('assert');
67
const resolvedPromise = SafePromise.resolve();
78

@@ -81,7 +82,12 @@ class ModuleJob {
8182
}
8283
throw e;
8384
}
84-
this.module.instantiate();
85+
try {
86+
this.module.instantiate();
87+
} catch (e) {
88+
decorateErrorStack(e);
89+
throw e;
90+
}
8591
for (const dependencyJob of jobsInGraph) {
8692
// Calling `this.module.instantiate()` instantiates not only the
8793
// ModuleWrap in this module, but all modules in the graph.

src/module_wrap.cc

+8
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
179179
}
180180

181181
void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
182+
Environment* env = Environment::GetCurrent(args);
182183
Isolate* isolate = args.GetIsolate();
183184
Local<Object> that = args.This();
184185
Local<Context> context = that->CreationContext();
185186

186187
ModuleWrap* obj = Unwrap<ModuleWrap>(that);
187188
CHECK_NE(obj, nullptr);
188189
Local<Module> module = obj->module_.Get(isolate);
190+
TryCatch try_catch(isolate);
189191
Maybe<bool> ok =
190192
module->InstantiateModule(context, ModuleWrap::ResolveCallback);
191193

@@ -195,6 +197,12 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
195197
obj->resolve_cache_.clear();
196198

197199
if (!ok.FromMaybe(false)) {
200+
CHECK(try_catch.HasCaught());
201+
CHECK(!try_catch.Message().IsEmpty());
202+
CHECK(!try_catch.Exception().IsEmpty());
203+
AppendExceptionLine(env, try_catch.Exception(), try_catch.Message(),
204+
ErrorHandlingMode::MODULE_ERROR);
205+
try_catch.ReThrow();
198206
return;
199207
}
200208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const foo = 'foo';
2+
export const bar = 'bar';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import { foo, notfound } from './module-named-exports';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Flags: --experimental-modules
2+
/* eslint-disable no-unused-vars */
3+
import '../common';
4+
import {
5+
foo,
6+
notfound
7+
} from '../fixtures/es-module-loaders/module-named-exports';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
file:///*/test/message/esm_display_syntax_error_import.mjs:6
3+
notfound
4+
^^^^^^^^
5+
SyntaxError: The requested module does not provide an export named 'notfound'
6+
at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*)
7+
at <anonymous>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Flags: --experimental-modules
2+
import '../common';
3+
import '../fixtures/es-module-loaders/syntax-error-import';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1
3+
import { foo, notfound } from './module-named-exports';
4+
^^^^^^^^
5+
SyntaxError: The requested module does not provide an export named 'notfound'
6+
at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*)
7+
at <anonymous>

0 commit comments

Comments
 (0)