在Vite中构建项目出错-Top-level await is not available in the configured target environme

365 阅读1分钟

前言

在项目中使用pnpm run dev时,出现了Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari13" + 2 overrides)报错。

过程

在网上的方法:

export default defineConfig({
  build: {
    target: 'esnext' 
  }
})

发现不能解决问题,依旧会报错,先补充一下新的方法:

解决方法

// vite.config.js
export default defineConfig({
  optimizeDeps: {
    esbuildOptions: {
      target: 'esnext'
    }
  },
  build: {
    target: 'esnext'
  },
  // more config options ...
})

已成功解决

参考资料

[](Vite 打包构建报错:Top-level await is not available in the configured... 的异常排查及解决方案 - 掘金) [](typescript - 在 Vite 中构建我的项目时出错 - 顶级 await 在配置的目标环境中不可用 - Stack Overflow)