Top-level await doesn't work in the latest Node.js

流过昼夜 提交于 2020-01-15 10:09:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!