diff --git a/src/node_contextify.cc b/src/node_contextify.cc index ec0f68671301d0..b35df7aa6f7e99 100644 --- a/src/node_contextify.cc +++ b/src/node_contextify.cc @@ -1406,7 +1406,9 @@ Local ContextifyContext::CompileFunctionAndCacheResult( constexpr std::array esm_syntax_error_messages = { "Cannot use import statement outside a module", // `import` statements "Unexpected token 'export'", // `export` statements - "Cannot use 'import.meta' outside a module"}; // `import.meta` references + "Cannot use 'import.meta' outside a module", // `import.meta` references + "await is only valid in async functions and the top level bodies of " + "modules"}; // top-level `await` void ContextifyContext::ContainsModuleSyntax( const FunctionCallbackInfo& args) { diff --git a/test/es-module/test-esm-detect-ambiguous.mjs b/test/es-module/test-esm-detect-ambiguous.mjs index 34c5f17f007c8f..3d2ee2cf53219a 100644 --- a/test/es-module/test-esm-detect-ambiguous.mjs +++ b/test/es-module/test-esm-detect-ambiguous.mjs @@ -3,9 +3,51 @@ import * as fixtures from '../common/fixtures.mjs'; import { spawn } from 'node:child_process'; import { describe, it } from 'node:test'; import { strictEqual, match } from 'node:assert'; +import { join } from 'node:path'; describe('--experimental-detect-module', { concurrency: true }, () => { describe('string input', { concurrency: true }, () => { + for (const { name, code, output } of [ + { + name: '`import` statements', + code: 'import { version } from "node:process"; console.log(version);', + output: `${process.version}\n`, + }, + { + name: '`export` statements', + code: 'export const foo = "bar"; console.log foo;', + output: 'bar\n', + }, + { + name: '`import.meta` references', + code: 'console.log(import.meta.filename);', + output: `${join(process.cwd(), '[eval1]')}\n`, + }, + { + name: 'top-level `await`', + code: 'const foo = await Promise.resolve("bar"); console.log(foo);', + output: 'bar\n', + }, + { + name: 'top-level `await` in a function call', + code: 'console.log(await Promise.resolve("bar"));', + output: 'bar\n', + }, + ]) { + it(`supports all possible ESM-only syntax elements: (${name})`, async () => { + const { stdout, stderr, code: exitCode, signal } = await spawnPromisified(process.execPath, [ + '--experimental-detect-module', + '--eval', + code, + ]); + + strictEqual(stderr, ''); + strictEqual(stdout, output); + strictEqual(exitCode, 0); + strictEqual(signal, null); + }); + } + it('permits ESM syntax in --eval input without requiring --input-type=module', async () => { const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [ '--experimental-detect-module',