顶级等待在构建中给出错误,但在开发中有效

The*_*Guy 2 node.js async-await typescript reactjs vite

我的代码在运行时可以工作npm run dev,但是当我构建它时,它会出现此错误。根据文档,我需要在 tsconfig.json 中将目标设置为 ES2017 或更高版本,但我正在使用 ESNEXT,我相信它是兼容的

错误 错误

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": false,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": ["./src"]
}
Run Code Online (Sandbox Code Playgroud)

The*_*Guy 6

因为我在问题中使用了 vite,所以我应该编辑 vite.config.ts 而不是 tsconfig.json

这是对它的修复 vite.config.ts

export default defineConfig({
  build: {
    minify: 'esbuild',
    target: "esnext"
  }
})
Run Code Online (Sandbox Code Playgroud)