Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const {
Array,
ArrayPrototypeFind,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeSome,
FunctionPrototype,
ObjectSetPrototypeOf,
PromisePrototypeThen,
Expand Down Expand Up @@ -65,8 +65,8 @@ const CJSGlobalLike = [
'__filename',
'__dirname',
];
const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
ArrayPrototypeSome(
const findCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
ArrayPrototypeFind(
CJSGlobalLike,
(globalLike) => errorMessage === `${globalLike} is not defined`,
);
Expand All @@ -79,11 +79,28 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
* @returns {void}
*/
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
if (e?.name === 'ReferenceError' &&
isCommonJSGlobalLikeNotDefinedError(e.message)) {
const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
if (notDefinedGlobalLike) {

if (hasTopLevelAwait) {
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.`;
let advice;
switch (notDefinedGlobalLike) {
case 'require':
advice = 'replace require() with import';
break;
case 'module':
case 'exports':
advice = 'use export instead of module.exports/exports';
break;
case '__filename':
advice = 'use import.meta.filename instead';
break;
case '__dirname':
advice = 'use import.meta.dirname instead';
break;
}

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}.`;
e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
return;
}
Expand Down
58 changes: 56 additions & 2 deletions test/es-module/test-esm-detect-ambiguous.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL },

assert.match(
stderr,
/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\./
/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\./
);
assert.strictEqual(stdout, '');
assert.strictEqual(code, 1);
Expand Down Expand Up @@ -440,7 +440,61 @@ describe('cjs & esm ambiguous syntax case', () => {

assert.match(
stderr,
/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\./
/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\./
);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
});

it('should throw an ambiguous syntax error when using top-level await with exports', async () => {
const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--eval',
`exports.foo = 'bar';\nawait 1;`,
]
);

assert.match(
stderr,
/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\./
);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
});

it('should throw an ambiguous syntax error when using top-level await with __filename', async () => {
const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--eval',
`console.log(__filename);\nawait 1;`,
]
);

assert.match(
stderr,
/ReferenceError: Cannot determine intended module format because both '__filename' 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 import\.meta\.filename instead\./
);

assert.strictEqual(code, 1);
assert.strictEqual(signal, null);
});

it('should throw an ambiguous syntax error when using top-level await with __dirname', async () => {
const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--eval',
`console.log(__dirname);\nawait 1;`,
]
);

assert.match(
stderr,
/ReferenceError: Cannot determine intended module format because both '__dirname' 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 import\.meta\.dirname instead\./
);

assert.strictEqual(code, 1);
Expand Down
Loading