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

Viewed 1182

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

1 Answers

First, check this; https://github.com/vitejs/vite/issues/6985. If you can't find an answer try this to create a big fat async function that executes itself to decrease the level of your await;

    (async () => {
    export const shopsData: shopType[] = await fetchStarbucksShops();
    export const countryGeoData: countryGeoDataType = await fetchGeoJsonCountry();
    .
    .
    .
    .
    .
     })();

It might not work. You should avoid top-level await somehow, whether use await inside the async function, or use .then()

Related