Skip to content
Closed
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
26 changes: 13 additions & 13 deletions lib/internal/modules/esm/module_job.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@
*/
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
if (e?.name === 'ReferenceError' &&
isCommonJSGlobalLikeNotDefinedError(e.message)) {
isCommonJSGlobalLikeNotDefinedError(e.message)) {

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.`;
const globalName = StringPrototypeSplit(e.message, ' ')[0];
e.message = `Cannot determine intended module format because both ${globalName} 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 ${globalName} with import.`;
e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
return;
}
Expand All @@ -96,9 +97,9 @@

const packageConfig =
StringPrototypeStartsWith(url, 'file://') &&
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
require('internal/modules/package_json_reader')
.getPackageScopeConfig(url);
RegExpPrototypeExec(/\.js(\?[^#]*)?(#.*)?$/, url) !== null &&
require('internal/modules/package_json_reader')
.getPackageScopeConfig(url);
if (packageConfig.type === 'module') {
e.message +=
'\nThis file is being treated as an ES module because it has a ' +
Expand Down Expand Up @@ -191,7 +192,7 @@
* @param {ModuleRequestType} requestType Type of the module request.
*/
constructor(loader, url, importAttributes = { __proto__: null }, moduleOrModulePromise,
phase = kEvaluationPhase, isMain, inspectBrk, requestType) {
phase = kEvaluationPhase, isMain, inspectBrk, requestType) {

Check failure on line 195 in lib/internal/modules/esm/module_job.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 14 spaces but found 4
super(loader, url, importAttributes, phase, isMain, inspectBrk);

// Expose the promise to the ModuleWrap directly for linking below.
Expand Down Expand Up @@ -308,8 +309,8 @@
// stack trace originates in module_job, not the file itself. A hidden
// symbol with filename could be set in node_errors.cc to facilitate this.
if (!getSourceMapsSupport().enabled &&
StringPrototypeIncludes(e.message,
' does not provide an export named')) {
StringPrototypeIncludes(e.message,
' does not provide an export named')) {

Check failure on line 313 in lib/internal/modules/esm/module_job.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 32 spaces but found 10
const splitStack = StringPrototypeSplit(e.stack, '\n', 2);
const { 1: childSpecifier, 2: name } = RegExpPrototypeExec(
/module '(.*)' does not provide an export named '(.+)'/,
Expand All @@ -336,9 +337,8 @@
` '${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` : ''}`;
`\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');
Expand Down Expand Up @@ -441,7 +441,7 @@
* first line paused in the debugger (because --inspect-brk is passed).
*/
constructor(loader, url, importAttributes, moduleWrap, phase = kEvaluationPhase, isMain,
inspectBrk, requestType) {
inspectBrk, requestType) {

Check failure on line 444 in lib/internal/modules/esm/module_job.js

View workflow job for this annotation

GitHub Actions / lint-js-and-md

Expected indentation of 14 spaces but found 4
super(loader, url, importAttributes, phase, isMain, inspectBrk);

this.module = moduleWrap;
Expand Down Expand Up @@ -492,7 +492,7 @@
}

assert.fail('Unexpected status of a module that is imported again after being required. ' +
`Status = ${status}`);
`Status = ${status}`);
}

runSync(parent) {
Expand Down
23 changes: 21 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 @@

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,26 @@

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 module', async () => {
const { stderr, code, signal } = await spawnPromisified(
process.execPath,
[
'--input-type=module',
'--eval',
`await 1;\nmodule.exports = 1;`,
]
);

assert.match(

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-macOS

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///Users/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04)

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///home/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / test-linux (ubuntu-24.04-arm)

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///home/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/node/node/node/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / aarch64-darwin: with shared libraries

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///Users/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / x86_64-linux: with shared libraries

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///home/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / aarch64-linux: with shared libraries

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///home/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /home/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs

Check failure on line 460 in test/es-module/test-esm-detect-ambiguous.mjs

View workflow job for this annotation

GitHub Actions / x86_64-darwin: with shared libraries

--- stdout --- Test failure: 'should throw an ambiguous syntax error when using top-level await with module' Location: test/es-module/test-esm-detect-ambiguous.mjs:450:3 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /ReferenceError: Cannot determine intended module format because both module 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 module with import\./. Input: '' at TestContext.<anonymous> (file:///Users/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs:460:12) at async Test.run (node:internal/test_runner/test:1102:7) at async Suite.processPendingSubtests (node:internal/test_runner/test:777:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '', expected: /ReferenceError: Cannot determine intended module format because both module 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 module with import\./, operator: 'match', diff: 'simple' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout /Users/runner/work/_temp/node-v26.0.0-nightly2025-12-1013cbc58974-slim/test/es-module/test-esm-detect-ambiguous.mjs
stderr,
/ReferenceError: Cannot determine intended module format because both module 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 module with import\./
);

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