问题
Is top-level await
still not supported in Node.js (Jan 2020, Node.js 13.5.0)?
I've tried some tutorials, like this one, but still no luck, always getting the same error:
D:\NodeJS>node --experimental-modules test.js
(node:17724) ExperimentalWarning: The ESM module loader is experimental.
file:///D:/NodeJS/test.js:9
await test();
^^^^^
SyntaxError: Unexpected reserved word
The entire file content:
function test() {
}
await test();
I have tried using "type": "module"
in package.json
, and renaming file into test.mjs
, but still the same error, even with the latest Node.js 13.5.0
What am I doing wrong?
回答1:
Per this issue tracker and this blog post, top-level await is available in Node v13.3+ behind the flag --harmony-top-level-await
. The module flag you're enabling is only for ESM modules and not for top level await.
回答2:
I think you are missing the async annotation
async function test() {
}
await test();
来源:https://stackoverflow.com/questions/59585793/top-level-await-doesnt-work-in-the-latest-node-js