在最新的Node.js中无法进行顶层等待 [英] Top-level await doesn't work in the latest Node.js

查看:580
本文介绍了在最新的Node.js中无法进行顶层等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js是否仍不支持顶级await(2020年1月,Node.js 13.5.0)?

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

整个文件内容:

function test() {
}

await test();

我尝试在package.json中使用"type": "module",并将文件重命名为test.mjs,但即使使用最新的Node.js 13.5.0,仍然会出现相同的错误

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

我在做什么错了?

推荐答案

每个此博客文章,节点v13.3 +中位于标志--harmony-top-level-await后的顶级await可用.您启用的模块标志仅适用于ESM模块,不适用于顶层等待.

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.

这篇关于在最新的Node.js中无法进行顶层等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆