Skip to content

Conversation

@mag123c
Copy link

@mag123c mag123c commented Oct 23, 2025

Fixes: #60322

Improve ERR_AMBIGUOUS_MODULE_SYNTAX error messages to show the actual undefined global instead of always mentioning require().

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. labels Oct 23, 2025
Comment on lines +80 to +81
const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {
const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
if (notDefinedGlobalLike) {

'--input-type=module',
'--eval',
`await 1;\nconst fs = require('fs');`,
`const fs = require('fs');\nawait 1;`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed the order to match the pattern in the existing test at line 278 (const fs = require("node:fs"); await Promise.resolve();). However, I realize this might be an unnecessary change if the original order was intentional. Should I revert this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think it should be reverted, both order should be supported so it makes sense to test both IMO

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revert changes.

const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--input-type=module',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed --input-type=module because I saw that line 278 already tests the same error without the flag:

const fs = require("node:fs"); await Promise.resolve();

I thought having both tests (with and without --input-type=module) was redundant since they produce the same error message. However, if there's a specific reason to test the explicit module type specification scenario separately, I can revert this change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've revert changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you have and you forgot to push your changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already there. I had removed it in an earlier commit, but reverted it back in 9457af9.

- Refactor getUndefinedCJSGlobalLike to use ArrayPrototypeFind
- Change if-else chain to switch statement
- Add single quotes around global names in error messages
- Revert test file to use --input-type=module flag and original order
- Update test regex patterns to expect quoted global names
ArrayPrototypeFind(
CJSGlobalLike,
(globalLike) => errorMessage === `${globalLike} is not defined`,
) ?? null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not useful, it it?

Suggested change
) ?? null;
);

Copy link
Author

@mag123c mag123c Oct 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.. It's redundant since ArrayPrototypeFind returns undefined when not found.
Thanks for review. I applied it

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the ERR_AMBIGUOUS_MODULE_SYNTAX error message

5 participants