Skip to content

Commit 8241a5a

Browse files
mag123caduh95
authored andcommitted
esm: improve error messages for ambiguous module syntax
1 parent 025ade6 commit 8241a5a

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

lib/internal/modules/esm/module_job.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const {
44
Array,
5+
ArrayPrototypeFind,
56
ArrayPrototypeJoin,
67
ArrayPrototypePush,
7-
ArrayPrototypeSome,
88
FunctionPrototype,
99
ObjectSetPrototypeOf,
1010
PromisePrototypeThen,
@@ -65,8 +65,8 @@ const CJSGlobalLike = [
6565
'__filename',
6666
'__dirname',
6767
];
68-
const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
69-
ArrayPrototypeSome(
68+
const findCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
69+
ArrayPrototypeFind(
7070
CJSGlobalLike,
7171
(globalLike) => errorMessage === `${globalLike} is not defined`,
7272
);
@@ -79,11 +79,28 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
7979
* @returns {void}
8080
*/
8181
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
82-
if (e?.name === 'ReferenceError' &&
83-
isCommonJSGlobalLikeNotDefinedError(e.message)) {
82+
const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
83+
if (notDefinedGlobalLike) {
8484

8585
if (hasTopLevelAwait) {
86-
e.message = `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.`;
86+
let advice;
87+
switch (notDefinedGlobalLike) {
88+
case 'require':
89+
advice = 'replace require() with import';
90+
break;
91+
case 'module':
92+
case 'exports':
93+
advice = 'use export instead of module.exports/exports';
94+
break;
95+
case '__filename':
96+
advice = 'use import.meta.filename instead';
97+
break;
98+
case '__dirname':
99+
advice = 'use import.meta.dirname instead';
100+
break;
101+
}
102+
103+
e.message = `Cannot determine intended module format because both '${notDefinedGlobalLike}' 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, ${advice}.`;
87104
e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
88105
return;
89106
}

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },
283283

284284
assert.match(
285285
stderr,
286-
/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\./
286+
/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\./
287287
);
288288
assert.strictEqual(stdout, '');
289289
assert.strictEqual(code, 1);
@@ -432,15 +432,32 @@ describe('cjs & esm ambiguous syntax case', () => {
432432
const { stderr, code, signal } = await spawnPromisified(
433433
process.execPath,
434434
[
435-
'--input-type=module',
436435
'--eval',
437-
`await 1;\nconst fs = require('fs');`,
436+
`const fs = require('fs');\nawait 1;`,
438437
]
439438
);
440439

441440
assert.match(
442441
stderr,
443-
/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\./
442+
/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\./
443+
);
444+
445+
strictEqual(code, 1);
446+
strictEqual(signal, null);
447+
});
448+
449+
it('should throw an ambiguous syntax error when using top-level await with exports', async () => {
450+
const { stderr, code, signal } = await spawnPromisified(
451+
process.execPath,
452+
[
453+
'--eval',
454+
`exports.foo = 'bar';\nawait 1;`,
455+
]
456+
);
457+
458+
match(
459+
stderr,
460+
/ReferenceError: Cannot determine intended module format because both exports 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, use export instead of module\.exports\/exports\./
444461
);
445462

446463
assert.strictEqual(code, 1);

0 commit comments

Comments
 (0)