From 68f23435cfa404b43cf4f284dca6fbeab1903053 Mon Sep 17 00:00:00 2001 From: GiHoon1123 Date: Wed, 30 Sep 2026 14:18:22 +0900 Subject: [PATCH] esm: preserve syntax error locations in dynamic imports Signed-off-by: GiHoon1123 --- lib/internal/modules/esm/loader.js | 9 ++++++++- test/es-module/test-esm-syntax-error.mjs | 12 ++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/internal/modules/esm/loader.js b/lib/internal/modules/esm/loader.js index f92e87785ce1..a04573228db8 100644 --- a/lib/internal/modules/esm/loader.js +++ b/lib/internal/modules/esm/loader.js @@ -33,6 +33,7 @@ const { const { getOptionValue } = require('internal/options'); const { isURL, pathToFileURL } = require('internal/url'); const { + decorateErrorStack, getDeprecationWarningEmitter, kEmptyObject, } = require('internal/util'); @@ -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; diff --git a/test/es-module/test-esm-syntax-error.mjs b/test/es-module/test-esm-syntax-error.mjs index 036e1ba13aff..d2382f56c82e 100644 --- a/test/es-module/test-esm-syntax-error.mjs +++ b/test/es-module/test-esm-syntax-error.mjs @@ -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\^+/); + }); });