Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vite build error -- Transform failed with 3 errors - Top-level await is not available in the configured target environment #7969

Closed
1 task done
cliffordfajardo opened this issue Nov 10, 2023 · 8 comments

Comments

@cliffordfajardo
Copy link
Contributor

cliffordfajardo commented Nov 10, 2023

What version of Remix are you using?

2.2.0

Are all your remix dependencies & dev-dependencies using the same version?

  • Yes

Steps to Reproduce

app-config.ts

const getValue = async () => return 1;

export const some_value = await getValue()

server.ts

import {some_value} from './app-config.ts`

Then run npm run build

 195 modules transformed.
[vite:esbuild-transpile] Transform failed with 3 errors:
index.js:42:0: ERROR: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 3 overrides)
index.js:54:0: ERROR: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 3 overrides)
index.js:66:0: ERROR: Top-level await is not available in the configured target environment ("chrome87", "edge88", "es2020", "firefox78", "safari14" + 3 overrides)

Expected Behavior

remix's vite plugin should detect the tsconfig.json settings that a user has in their remix project.
I have "target": "ES2022", in my tsconfig.json but its not being used for the build step 🤔

@cliffordfajardo
Copy link
Contributor Author

cliffordfajardo commented Nov 10, 2023

I was able to solve this by adding this to my vite.config.ts file:

export default defineConfig({
  plugins: [
    remix({
      // appDirectory: "app",
      // assetsBuildDirectory: "public/build",
      // serverBuildPath: "build/index.js",
      // publicPath: "/build/",
      serverModuleFormat: "esm",
      ignoredRouteFiles: ["**/.*"],
      routes(defineRoutes) {
        return Promise.resolve(createRoutesFromFolders(defineRoutes));
      },
    }),
    tsconfigPaths(),
  ],
  build: {
    target: "ES2022" // <--------- ✅✅✅✅✅✅
  },
});

I used the ES2022 target value from my tsconfig.json file:

{
  "include": ["env.d.ts", "**/*.ts", "**/*.tsx"],
  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2022"],
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,
    "target": "ES2022",  // <--------- ✅✅✅✅✅✅
    "module": "ES2022",
    "strict": true,
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "skipLibCheck": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./app/*"]
    },

    // Remix takes care of building everything in `remix build`.
    "noEmit": true
  }
}

References

@pcattori @markdalgleish - maybe the remix vite plugin should use tsconfig settings a user has?

@cliffordfajardo cliffordfajardo changed the title Transform failed with 3 errors - Top-level await is not available in the configured target environment vite build error -- Transform failed with 3 errors - Top-level await is not available in the configured target environment Nov 10, 2023
@cliffordfajardo
Copy link
Contributor Author

cliffordfajardo commented Nov 10, 2023

The error message referenced firefox78, so I did a grep in node_modules folder for that string and saw it came from vite

cd node_modules
grep -r "firefox78"

./vite/dist/node/constants.js:    'firefox78',

then that gave me an indication it was coming from vite. But I should have probably known that since this error happens when calling npm run build 😄

package.json

"build": "vite build && vite build --ssr",

Then I did a google search for the error: vite Top-level await is not available in the configured target environment
and it was nice seeing vite community provide answers 🙏

@markdalgleish
Copy link
Member

markdalgleish commented Nov 13, 2023

Thanks for raising this.

This is strictly a Vite concern. We're wanting to keep Remix decoupled from your Vite config as much as possible, and this is a good example of something that you're free to configure yourself. If you want your Vite config to automatically sync with your tsconfig, this could be implemented as its own Vite plugin outside of Remix.

@SpencerDuball
Copy link

Just ran into this error with remix@2.5.1 while deploying with SST. The fix that worked for me was to set:

export default defineConfig({
  server: { port: 3000 },
  plugins: [remix(), tsconfigPaths()],
  optimizeDeps: { esbuildOptions: { target: "esnext" } }, // <-- Set this to resolve issue.
});

Got this solution from: mozilla/pdf.js#17245

@vmaubert
Copy link

Hi.

Can we have a build target for the server bundle and another one for the client bundle?
I need a top-await during the startup, and I don’t want to set target: esnext for the browsers…

Thx!

@cliffordfajardo
Copy link
Contributor Author

@vmaubert - you can try using the following fields to play around with the server and client builds:

In vite.config.ts's remix plugin, you can pass serverModuleFormat: "esm", similar to this:

export default defineConfig({
  plugins: [
    remix({
      // ⬇️ The output format of the server build. Defaults to "esm".
      serverModuleFormat: "esm",
      ignoredRouteFiles: ["**/.*"],
    }),
    tsconfigPaths(),
  ],
  build: {
    target: "ES2022",
  },
});

For anyone reading this using esbuild & not vite with remix, my recommendations:

  1. upgrade to vite
  2. reference older version of remix docs as some properties you pass to the remix vite plugin are named differently than the values you pass to the remix.config.js file
  3. Reference the Remix Changelog and do COMMAND+F search for related keywords for API changes/renames

Testing settings

If you want to play around with the settings, probably best to spin up a quick remix project using the CLI to test the settings

# create fresh remix app
npx create-remix --template remix-run/remix/templates/vite-express

# build the app
npm run build

ls build
client server

With vite, whats nice is the the client & server build are clearly separated once you build the app:
CleanShot 2024-02-22 at 04 22 14@2x

@cliffordfajardo
Copy link
Contributor Author

cliffordfajardo commented Feb 22, 2024

@vmaubert - i created a new remix discussion here since I still do find the serverModuleFormat and build top level vite options confusing

Would be good to get your input 🙏
Github Discussion: #8856

@luzat
Copy link

luzat commented Mar 16, 2024

@vmaubert Selecting a different build target the server build (SSR) seems to work like that:

export default defineConfig(({ isSsrBuild }) => ({
  plugins: [
    remix(),
    tsconfigPaths(),
  ],
  build: isSsrBuild ? { target: 'ES2022' } : {},
}))

This allows me to use top-level await in server code while not messing with browser code.

henghaopu added a commit to henghaopu/henghaopu.com that referenced this issue Jun 28, 2024
Top-level await is not available in the configured target environment
remix-run/remix#7969
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants