Improve the ERR_AMBIGUOUS_MODULE_SYNTAX error messageΒ #60322
Copy link
Copy link
Closed
ST4RKJR/node
#1Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
Description
module.exports;
await 1;The above code snippet causes an ERR_AMBIGUOUS_MODULE_SYNTAX error, since module.exports cannot be used (CJS) in an ESM file. However, the error logged to the console, shown below, incorrectly assumes the error stems from the use of a require()
ReferenceError: Cannot determine intended module format because both require() and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, replace require() with import.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
esmIssues and PRs related to the ECMAScript Modules implementation.Issues and PRs related to the ECMAScript Modules implementation.good first issueIssues that are suitable for first-time contributors.Issues that are suitable for first-time contributors.moduleIssues and PRs related to the module subsystem.Issues and PRs related to the module subsystem.
Activity
avivkeller commentedon Oct 19, 2025
A more fitting error would be
aduh95 commentedon Oct 19, 2025
There's no such thing as "CJS syntax", it's just JavaScript β in this case, if the file cannot be loaded as ESM is not a syntax problem, but the use of an undeclared identifier that does not map to a global.
The current message is more more correct β except that it mentions
require()instead ofmodule.joyeecheung commentedon Oct 20, 2025
I agree with @aduh95 - the proposed version wasn't correct either. The correction should probably be something like
(i.e. changing
require()tomodule, or if it'sexports,__filename__etc., adjust accordingly).joyeecheung commentedon Oct 20, 2025
Marking it as a first good issue, I think it can be fixed by updating
node/lib/internal/modules/esm/module_job.js
Lines 78 to 85 in c742220
And replace
require()in the message with what gets detected byisCommonJSGlobalLikeNotDefinedError()as the undefined global. Also, the last instruction should change depending on exactly what undefined global is referenced.require/__filename__/__dirname__may have alternatives in ESM, butmodule.exports/exportsdon't necessarily (only writes to them have equivalents, reads to them do not)tysoncung commentedon Oct 21, 2025
I noticed this issue. Have you tried checking the error logs or console output? That might help narrow down the root cause. I'd be happy to help investigate if you can share more details about your environment (OS, version, etc.).
mag123c commentedon Oct 23, 2025
I'd like to work on this issue. I'll update the error message in
lib/internal/modules/esm/module_job.jsto reference the actual undefined global as suggested.--
I've opened PR. The changes include:
Mishradity commentedon Nov 12, 2025
can you assign it to me
Pragati-cse commentedon Dec 5, 2025
Hi , I'm Pragati. i did like to work on this issue and improve the messaga . can you assign to me ? thanks.
avivkeller commentedon Dec 5, 2025
We don't assign issues
esm: improve error messages for ambiguous module syntax
esm: improve error messages for ambiguous module syntax
esm: improve error messages for ambiguous module syntax
esm: improve error messages for ambiguous module syntax
esm: improve error messages for ambiguous module syntax