node.jsreactjstypescriptasync-awaitvite

Top level await gives error in build but works on dev


My code works when ran npm run dev but when I build it it gives this error. According to the docs I need target set to ES2017 or higher in tsconfig.json but I am using ESNEXT which I believe is compatible

error error

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"]
}

Solution

  • Because I was using vite in the question, vite.config.ts is what I should have been looking to edit instead of tsconfig.json

    here is the fix to it vite.config.ts

    export default defineConfig({
      build: {
        minify: 'esbuild',
        target: "esnext"
      }
    })