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: https://github.com/nodejs/node/pull/17786
Fixes: https://github.com/nodejs/node/issues/17785
Refs: https://github.com/nodejs/node/pull/17281
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Michaël Zasso authored and Myles Borins committed 2018-01-09 04:32:35 -05:00
1 parent 14eb97ebf7
commit 3b2d8cba23
8 files changed
+42 -1

No files matched your search

+7 -1
View File
@@ -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.
+8
View File
@@ -179,6 +179,7 @@ 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();
@@ -186,6 +187,7 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
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);
@@ -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;
}
}
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
@@ -0,0 +1 @@
import { foo, notfound } from './module-named-exports';
@@ -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';
@@ -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>
@@ -0,0 +1,3 @@
// Flags: --experimental-modules
import '../common';
import '../fixtures/es-module-loaders/syntax-error-import';
@@ -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>