Top level async in node scripts

Write async-await in mjs files and run as scripts

Profile pictureToni Petrina
Published on 2022-02-051 min read
  • #node

To run async code in a script, use:

  • mjs extension
  • NODE_OPTIONS='--experimental-json-modules'

Inside package.json:

"script-name": "NODE_OPTIONS='--experimental-json-modules' && node ./path/to/script.mjs",

Then write any kind of async code in the top level:

await fs.writeFile("file");

Change code theme: