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

3

86

I am going through Stripes integration steps and have come across an error for my code found in step 2.1 (https://stripe.com/docs/connect/collect-then-transfer-guide#create-an-account-link)

How do I fix this error?

Code:

const stripe = require('stripe')('someID');
const account = await stripe.accounts.create({
  type: 'express',
});

Error:

Top-level 'await' expressions are only allowed when the 'module' option is set to 'esnext' or 'system', and the 'target' option is set to 'es2017' or higher.ts(1378)

Hugely answered 5/3, 2021 at 4:43 Comment(9)
which means you need to wrap your await expression with async function or do some extra configuration to support top level await.Debrahdebrecen
Apparently even if you change your configuration, tsc ignores your tsconfig.json if you pass files explicitly.Flamboyant
@Debrahdebrecen that's not true, you should be able to use top level await (ie with no wrapper async function) as the error mentions, provided the tsconfig.json options are correct.Indemnify
@Flamboyant do you have a reference for that?Indemnify
@Indemnify no, I found that out the hard way. :/Flamboyant
@Flamboyant I did some research and found a reference for it (and a workaround)- I’ve added an answer below with two solutions.Indemnify
Does this answer your question? Using await outside of an async functionSubhead
@kev can you please select a different answer? The currently selected answer doesn't actually solve the issue.Indemnify
use bun, it worked perfectly for meBennet
D
45

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://mcmap.net/q/36252/-how-can-i-use-async-await-at-the-top-level

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.

Debrahdebrecen answered 5/3, 2021 at 5:2 Comment(3)
This is a workaround showing the user how to avoid using top level await, rather than an answer. As the error mentions, you should be able to use top level await (ie with no wrapper async function) as the error mentions, provided the tsconfig.json options are correct.Indemnify
I fail to see how this is even relevant as it produces a promise, therefore this is the same as removing await or I am missing something?Linsk
@Linsk You're not missing anything. In fact this answer is just bad and should not be upvoted as it does not solve anything. You might as well just do const createPromise = stripe.accounts.create(...) It's the same thing but without a wrapper.Invaginate
I
54

To actually use top level await (i.e. without wrappers)

I'm going to describe the cause and solution to your problem with tsc, but also show you a much better way to run .ts files from the command line.

Here's something you may have missed:

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

It's also mentioned in --help but I do agree it's a bit unintuitive:

$ 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.

Solution 1 - specify a ts file explicitly and use command line args to provide the right 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.

Solution 3: use a better tool like esrun

esrun is a newer tool than just runs ts files with no prior setup. Unlike tsc, ts-node, tsx etc, esrun:

  • Does not need a tsconfig.json
  • Does not need package.json
  • Supports top level await
  • Does not give silly error messages (like ts-node saying it doesn't support running ts files)
npm i esrun

Then just:

npx esrun file.ts

No other setup is needed.

Indemnify answered 18/3, 2022 at 11:17 Comment(3)
bun worked perfectly for meBennet
@DzmitryLahoda sure but bun has many significant differences to node.jsIndemnify
Solution 2 works with .ts > .js as long as you have "type": "module", in your package.json. Or use the .mts extension to get a .mjs file without it!Zootoxin
D
45

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://mcmap.net/q/36252/-how-can-i-use-async-await-at-the-top-level

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.

Debrahdebrecen answered 5/3, 2021 at 5:2 Comment(3)
This is a workaround showing the user how to avoid using top level await, rather than an answer. As the error mentions, you should be able to use top level await (ie with no wrapper async function) as the error mentions, provided the tsconfig.json options are correct.Indemnify
I fail to see how this is even relevant as it produces a promise, therefore this is the same as removing await or I am missing something?Linsk
@Linsk You're not missing anything. In fact this answer is just bad and should not be upvoted as it does not solve anything. You might as well just do const createPromise = stripe.accounts.create(...) It's the same thing but without a wrapper.Invaginate
E
-4

you can just use a sample promise. and it will work.

new Promise(async(resolve, reject) => { const res = await resPromise  })
Eustoliaeutectic answered 23/9, 2023 at 20:20 Comment(1)
This makes no sense. You should never pass an async function as the executor to new Promise! Also why are you even constructing a new Promise when you already have a resPromise?Regulus

© 2022 - 2024 — McMap. All rights reserved.