Top-level await is not available in the configured target environment ("es2015" + 2 overrides)
时间: 2024-04-13 12:29:47 浏览: 15
Top-level await is a feature that allows you to use the `await` keyword at the top level of your code, outside of an `async` function. However, it is not available in all JavaScript environments. The error message you're seeing indicates that your current target environment does not support top-level await.
To resolve this issue, you have a few options:
1. Use a different target environment: You can configure your environment to target a version of JavaScript that supports top-level await, such as "es2017" or later. This depends on your build tool or environment configuration.
2. Wrap your code in an async function: If you can't change the target environment, you can wrap your code in an immediately invoked async function expression (IIFE) to use top-level await. Here's an example:
```javascript
(async () => {
// Your code here
await someAsyncTask();
})();
```
3. Use a bundler or transpiler: If you're using a bundler like Webpack or a transpiler like Babel, you can configure it to transform your code to a version of JavaScript that supports top-level await. This way, you can write code with top-level await and have it automatically transformed for the target environment.
Remember to choose an option that fits your project's requirements and constraints.