typescriptvite

Get error to build my project in Vite - Top-level await is not available in the configured target environment


I try to build my project in vite,

my project - https://github.com/yakovcohen4/starbucks-openlayers

I run npm run dev and all work.

but when I run to build it I get an error.

error message: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2019", "firefox78", "safari13.1")

I try to fetch a data and think here is the problem link (line 22+23) - https://github.com/yakovcohen4/starbucks-openlayers/blob/main/starbucks-project/src/main.ts

const shopsData = await fetchStarbucksShops();

If anyone encounters this curse I would be happy to help


Solution

  • For those who experience the same issue in the both dev and build commands, add the following

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