我正在经历Stripes集成步骤,并且遇到了步骤2.1 (https://stripe.com/docs/connect/collect-then-transfer-guide#create-an-account-link)中发现的代码错误
如何修复此错误?
代码:
const stripe = require('stripe')('someID');
const account = await stripe.accounts.create({
type: 'express',
});
错误:
顶级'await'表达式只允许在'module'选项设置为"esnext"或"system",并设置"target"选项到'es2017'或更高版本。ts(1378)
你可以将const account的代码包装在async函数中,因为你的目标选项不支持顶级await。
const account = async () => {
await stripe.accounts.create({
type: "express",
});
};
这取决于你的代码是想返回一些东西还是想在await之后执行一些其他任务。
Incase如果你想使用top - level await,更多关于top - level await的信息请访问https://stackoverflow.com/a/56590390/9423152
这只是一个解决问题的方法,而不是其他用户提到的确切解决方案。此外,如果你在node上使用Typescript,你可以尝试改变tsconfig文件中的模块选项和目标。
实际使用顶层等待(即不使用包装器)
你可能错过了一些东西:
tsc
在提供文件名编译时忽略tsconfig.json
中的配置
它也在--help
中提到,但我同意它有点不直观:
$ 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.
解决方案1—显式指定ts
文件并使用命令行参数来提供正确的选项:
所以你需要使用:
npx tsc -t es2022 -m es2022 --moduleResolution node --outDir dist src/runme.mts
解决方案2 -使用tsc
在tsconfig.json
中使用src
指定.ts文件下面是对顶级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"]
}
确保您的tsconfig中有include
文件夹。Json包含使用顶级await:
npx tsc
生成dist/runme.mjs
并运行编译后的应用程序。