仅当“module”选项设置为“esnext”时才允许使用顶级“await”表达式
Top-level ‘await’ expressions are only allowed when the ‘module’ option is set to ‘esnext'
我正在执行 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',
});
错误:
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)
您可以将 const 帐户的代码包装在异步函数中,因为您的目标选项不支持顶级等待。
const account = async () => {
await stripe.accounts.create({
type: "express",
});
};
这取决于您的代码是要return 某事还是要在 await 之后执行一些其他任务。
如果你想使用顶级等待,更多关于使用顶级等待的信息在
这只是问题的解决方法,而不是其他用户提到的确切解决方案。
此外,
如果您在节点上使用 Typescript,您可以尝试更改 tsconfig 文件中的模块选项和目标。
修复错误并使用顶级 await
解决方案 2 - 使用 tsc
而不指定 .ts 文件,使用 tsconfig.json
这是一个正确设置顶级等待的配置:
{
// 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.json 中的 include
文件夹包含使用顶级 await 的打字稿**:
npx tsc
(dist/runme.mjs 已生成并且我编译的应用程序运行)
解决方案 2 - 指定一个 ts
文件并使用命令行参数提供正确的选项:
tsc
在提供要编译的文件名时忽略 tsconfig.json
中的配置。
$ 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.
所以你需要使用:
npx tsc -t es2022 -m es2022 --moduleResolution node --outDir dist src/runme.mts
我正在执行 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',
});
错误:
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)
您可以将 const 帐户的代码包装在异步函数中,因为您的目标选项不支持顶级等待。
const account = async () => {
await stripe.accounts.create({
type: "express",
});
};
这取决于您的代码是要return 某事还是要在 await 之后执行一些其他任务。
如果你想使用顶级等待,更多关于使用顶级等待的信息在
这只是问题的解决方法,而不是其他用户提到的确切解决方案。 此外, 如果您在节点上使用 Typescript,您可以尝试更改 tsconfig 文件中的模块选项和目标。
修复错误并使用顶级 await
解决方案 2 - 使用 tsc
而不指定 .ts 文件,使用 tsconfig.json
这是一个正确设置顶级等待的配置:
{
// 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.json 中的 include
文件夹包含使用顶级 await 的打字稿**:
npx tsc
(dist/runme.mjs 已生成并且我编译的应用程序运行)
解决方案 2 - 指定一个 ts
文件并使用命令行参数提供正确的选项:
tsc
在提供要编译的文件名时忽略 tsconfig.json
中的配置。
$ 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.
所以你需要使用:
npx tsc -t es2022 -m es2022 --moduleResolution node --outDir dist src/runme.mts