21

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)

Kev Wats
  • 446
  • 1
  • 3
  • 12
  • which means you need to wrap your await expression with async function or do some extra configuration to support top level await. – Bibek Mar 05 '21 at 04:49
  • 1
    Apparently even if you change your configuration, `tsc` ignores your `tsconfig.json` if you pass files explicitly. – weberc2 Nov 17 '21 at 03:43
  • @Bibek 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. – mikemaccana Feb 09 '22 at 15:46
  • @weberc2 do you have a reference for that? – mikemaccana Mar 18 '22 at 11:11
  • @mikemaccana no, I found that out the hard way. :/ – weberc2 Mar 19 '22 at 21:24
  • @weberc2 I did some research and found a reference for it (and a workaround)- I’ve added an answer below with two solutions. – mikemaccana Mar 21 '22 at 16:50
  • Does this answer your question? [Using await outside of an async function](https://stackoverflow.com/questions/39679505/using-await-outside-of-an-async-function) – Liam Apr 11 '22 at 15:43

2 Answers2

16

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.

Bibek
  • 595
  • 1
  • 7
  • 20
  • On the link you provided, #1 worked for me. I had to wrap it in an async function. – Gabriel Arghire Jun 23 '21 at 15:33
  • 8
    **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. – mikemaccana Feb 09 '22 at 15:48
  • 3
    This is not an answer to the question OP posted. – inoabrian Feb 27 '22 at 20:17
2

To fix the error and use top level await

Solution 2 - use tsc without specifying a .ts file, using a 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 2 - specify a ts file and use command line args to provide the right options:

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

$ 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
mikemaccana
  • 94,893
  • 84
  • 350
  • 433