Node: Broken support for Top-Level Await

Created on 20 May 2020  路  11Comments  路  Source: nodejs/node

  • Version: v14.3.0
  • Platform: Windows 10 64-bit

What steps will reproduce the bug?

// test.js
await Promise.resolve(1);

```bash
$ node ./test.js

### What is the expected behavior?


```bash
$ node ./test.js

What do you see instead?

$ node ./test.js
C:\workspace\test.js:2
await Promise.resolve(1);
^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:1116:16)
    at Module._compile (internal/modules/cjs/loader.js:1164:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Most helpful comment

I'm trying to figure out if there is something actionable here. Do we need to update docs or changelog? Should we close this?

All 11 comments

This feature is behind the --experimental-top-level-await flag. See https://github.com/nodejs/node/pull/33473.for updating the documentation.

Even with node --experimental-top-level-await test-await.js, it is not working.

=> SyntaxError: await is only valid in async function

It will only work for esm modules. The default is to use the cjs loader. If you rename .js to .mjs it'll work properly. It will also work if you define the loader differently.

It will only work for esm modules. The default is to use the cjs loader. If you rename .js to .mjs it'll work properly. It will also work if you define the loader differently.

Could it work with the REPL ? (any flag to make REPL context a module context ?)

@8HoLoN we have experimental top level await in the REPL but it's not working identical to the "regular" top level await that's implemented in v14.3.0. You have to use the --experimental-repl-await flag to activate it.

Activating ESM is not yet supported in the REPL.

Changelog is misleading. There is nothing about flag:
obraz
Unfortunetlly new flag was added in quickly:

--experimental-top-level-await is not allowed in NODE_OPTIONS

I'm trying to figure out if there is something actionable here. Do we need to update docs or changelog? Should we close this?

We could also catch the TLA error in the CJS goal and give a better error message

At minimum I would say, the mandatory flag should be mentioned in changelog and docs. (And that it is only working in top level of es module context)

This got fixed by #33473

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mcollina picture mcollina  路  3Comments

fanjunzhi picture fanjunzhi  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

loretoparisi picture loretoparisi  路  3Comments

jmichae3 picture jmichae3  路  3Comments