Skip to content
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
9 changes: 8 additions & 1 deletion lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const {
const { getOptionValue } = require('internal/options');
const { isURL, pathToFileURL } = require('internal/url');
const {
decorateErrorStack,
getDeprecationWarningEmitter,
kEmptyObject,
} = require('internal/util');
Expand Down Expand Up @@ -438,7 +439,13 @@ class ModuleLoader {
throw new ERR_UNKNOWN_MODULE_FORMAT(translatorKey, url);
}

const result = FunctionPrototypeCall(translator, this, url, translateContext, parentURL);
let result;
try {
result = FunctionPrototypeCall(translator, this, url, translateContext, parentURL);
} catch (error) {
decorateErrorStack(error);
throw error;
}
assert(result instanceof ModuleWrap, `The ${format} module returned is not a ModuleWrap`);
if (format === 'commonjs' || format === 'commonjs-sync' || format === 'require-commonjs') {
result.isCommonJS = true;
Expand Down
12 changes: 12 additions & 0 deletions test/es-module/test-esm-syntax-error.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ describe('ESM: importing a module with syntax error(s)', { concurrency: !process
assert.match(stderr, /SyntaxError:/);
assert.notStrictEqual(code, 0);
});

it('should include the source location when imported dynamically', async () => {
const moduleURL = fixtures.fileURL('es-module-loaders', 'syntax-error.mjs');
const { code, stderr } = await spawnPromisified(execPath, [
'-e',
`import(${JSON.stringify(moduleURL.href)}).catch(console.error)`,
]);

assert.strictEqual(code, 0);
assert.match(stderr, /syntax-error\.mjs:2/);
assert.match(stderr, /await async \(\) => 0;\n\^+/);
});
});
Loading