Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: print better message on esm import error #17786

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/internal/loader/ModuleJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
}

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

ModuleWrap* obj = Unwrap<ModuleWrap>(that);
CHECK_NE(obj, nullptr);
Local<Module> module = obj->module_.Get(isolate);
TryCatch try_catch(isolate);
Maybe<bool> ok =
module->InstantiateModule(context, ModuleWrap::ResolveCallback);

Expand All @@ -195,6 +197,12 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& 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;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/es-module-loaders/module-named-exports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/syntax-error-import.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { foo, notfound } from './module-named-exports';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import.mjs
Original file line number Diff line number Diff line change
@@ -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';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import.out
Original file line number Diff line number Diff line change
@@ -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 <anonymous>
3 changes: 3 additions & 0 deletions test/message/esm_display_syntax_error_import_module.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Flags: --experimental-modules
import '../common';
import '../fixtures/es-module-loaders/syntax-error-import';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import_module.out
Original file line number Diff line number Diff line change
@@ -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 <anonymous>