Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext’

We Are Going To Discuss About Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext’. So lets Start this Javascript Article.

Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext’

How to solve Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext’

You can wrap your code for const account inside an async function as your target option doesn’t support top level await.
const account = async () => { await stripe.accounts.create({ type: "express", }); };
It depends on your code whether you want to return something or you want to perform some other tasks after await.
Incase if you want to use top level await, More about using top level await is on https://stackoverflow.com/a/56590390/9423152
This it just a workaround of the problem not the exact solution as referred by other users.
Furthermore,
You can try changing the module option and target in tsconfig file if you are using Typescript on node.

Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext’

You can wrap your code for const account inside an async function as your target option doesn’t support top level await.
const account = async () => { await stripe.accounts.create({ type: "express", }); };
It depends on your code whether you want to return something or you want to perform some other tasks after await.
Incase if you want to use top level await, More about using top level await is on https://stackoverflow.com/a/56590390/9423152
This it just a workaround of the problem not the exact solution as referred by other users.
Furthermore,
You can try changing the module option and target in tsconfig file if you are using Typescript on node.

Solution 1

You can wrap your code for const account inside an async function as your target option doesn’t support top level await.

 const account = async () => {
        await stripe.accounts.create({
          type: "express",
  });
};

It depends on your code whether you want to return something or you want to perform some other tasks after await.

Incase if you want to use top level await, More about using top level await is on https://stackoverflow.com/a/56590390/9423152

This it just a workaround of the problem not the exact solution as referred by other users.
Furthermore,
You can try changing the module option and target in tsconfig file if you are using Typescript on node.

Original Author Bibek Of This Content

Solution 2

To fix the error and use top level await

Here’s something you may have missed:

tsc ignores the configuration in tsconfig.json when provided with a file name to compile.

Solution 1 – specify a ts file explicitly and use command line args to provide the right options:

$ npx tsc --help
tsc: The TypeScript Compiler - Version 4.6.2
                                                                                                                     TS
COMMON COMMANDS

  ....

  tsc app.ts util.ts
  Ignoring tsconfig.json, compiles the specified files with default compiler options.

So you’ll need to use:

npx tsc -t es2022 -m es2022 --moduleResolution node --outDir dist src/runme.mts

Solution 2 – use tsc specifying the .ts file using src in tsconfig.json

Here’s a config with the correct settings for top level await:

{
  // https://www.typescriptlang.org/tsconfig#compilerOptions
  "compilerOptions": {
    "esModuleInterop": true,
    "lib": ["es2020"],
    "module": "es2022",
    "preserveConstEnums": true,
    "moduleResolution": "node",
    "strict": true,
    "sourceMap": true,
    "target": "es2022",
    "types": ["node"],
    "outDir": "dist"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

Make sure the include folders in your tsconfig.json contain your typescript that uses top level await:

npx tsc

dist/runme.mjs is generated and my compiled app runs.

Original Author mikemaccana Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment