Top-level await does not work with node 14.13.-

Top-level await only works with ESM modules (JavaScript’s own module format), not with Node.js’s default CommonJS modules. From your stack trace, you’re using CommonJS modules.

You need to put "type": "module" in package.json or use .mjs as the file extension (I recommend using the setting).

For instance, with this package.json:

{
  "type": "module"
}

and this main.js:

const x = await Promise.resolve(42);
console.log(x);

node main.js shows 42.


Side note: You don’t need --harmony-top-level-await with v14.13.0. Top-level await is enabled by default in that version (it was enabled in v14.8.0).

Leave a Comment