Repository navigation
detect-module: confusing error when parsing a CommonJS module with top-level await #55776
Copy link
Copy link
Closed
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
Description
Activity
- addedesmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.
on Nov 8, 2024 - addedmoduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
on Nov 8, 2024 - changed the title
[-]Module syntax detection: confusing error when parsing a CommonJS module with top-level `await`[/-][+]detect-module: confusing error when parsing a CommonJS module with top-level `await`[/+]on Nov 8, 2024 Hi, I've looked around a bit to work on this and I wonder where I can find the CommonJS parsing error.
https://github.com/nodejs/node/blob/main/lib/internal/modules/esm/module_job.js#L286
I think it's here: 216. line
if (format === 'commonjs') { const importStatement = splitStack[1]; // TODO(@ctavan): The original error stack only provides the single // line which causes the error. For multi-line import statements we // cannot generate an equivalent object destructuring assignment by // just parsing the error stack. const oneLineNamedImports = RegExpPrototypeExec(/{.*}/, importStatement); const destructuringAssignment = oneLineNamedImports && RegExpPrototypeSymbolReplace(/\s+as\s+/g, oneLineNamedImports, ': '); e.message = `Named export '${name}' not found. The requested module` + ` '${childSpecifier}' is a CommonJS module, which may not support` + ' all module.exports as named exports.\nCommonJS modules can ' + 'always be imported via the default export, for example using:' + `\n\nimport pkg from '${childSpecifier}';\n${ destructuringAssignment ? `const ${destructuringAssignment} = pkg;\n` : ''}`; const newStack = StringPrototypeSplit(e.stack, '\n'); newStack[3] = `SyntaxError: ${e.message}`; e.stack = ArrayPrototypeJoin(newStack, '\n'); }
I wonder where I can find the CommonJS parsing error.
https://github.com/nodejs/node/blob/main/src/node_contextify.cc#L1613-L1614
Reacted by Mert Can AltinHello, I tried to make a small improvement for this place, I would be very happy if you review it at the appropriate time.
- added a commit that references this issue
on Jun 24, 2025 - added a commit that references this issue
on Jul 3, 2025 - added 2 commits that reference this issue
on Jul 21, 2025
Metadata
Metadata
Assignees
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
Porting from nodejs/TSC#1445 (comment):
index.js:Getting this:
With this
package.json:{ "dependencies": { "express": "4.21.0", "get-port-please": "3.1.2" }, "devDependencies": { "@types/express": "5.0.0" } }LOL. (
await😈).Originally posted by @babakfp in nodejs/TSC#1445 (comment)
So basically, this module fails to parse as either CommonJS or as ESM, and we show the ESM parsing error message. Perhaps we should show both, or show a special message for the common use case of using top-level
awaitin a CommonJS module.