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

Top level await declarations prevent module typed compilation #2269

Open
j3ffgray opened this issue Jun 24, 2024 · 5 comments
Open

Top level await declarations prevent module typed compilation #2269

j3ffgray opened this issue Jun 24, 2024 · 5 comments

Comments

@j3ffgray
Copy link

Behavior

Deploying a project using a framework like Svelte requires a package.json set to "type: "module".

ESM modules don't support top level await functions, two of which are present in GeminiKit.

Steps to Reproduce the Problem

  1. Using sveltekit and svelte-adapter-appengine, deploy to App Engine.
  2. This causes the error noted below.
  3. Running locally works as intended, perhaps a vite configuration tweak could solve the issue

I haven't found a solution yet, and I'm not familiar enough with the structure of the kits yet, so I don't have a fix but wanted to note it, as it prevents deploying experiments with certain OS tools.

Specifications

  • Version: Svelte Kit 2.0.0, Svelte 4.2.7, Vite 5.0.3, Svelte Adapter Appengine 1.1.0

Error response when running npm run build

> Using svelte-adapter-appengine
✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16:
      11 │ const adapter = await GraphToKitAdapter.create(kit, KIT_BASE_URL, []);
         ╵                 ~~~~~

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format

    node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15:
      66 │ export default await board(() => {
         ╵                ~~~~~

error during build:
Error: Build failed with 2 errors:
node_modules/@google-labs/gemini-kit/dist/src/boards/gemini-pro-vision.js:66:15: ERROR: Top-level await is currently not supported with the "cjs" output format
node_modules/@google-labs/gemini-kit/dist/src/index.js:11:16: ERROR: Top-level await is currently not supported with the "cjs" output format
    at failureErrorWithLog (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1651:15)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1059:25
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1004:52
    at buildResponseToResult (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1057:7)
    at /Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:1086:16
    at responseCallbacks.<computed> (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:704:9)
    at handleIncomingPacket (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:764:9)
    at Socket.readFromStdout (/Users/jeffgray/Dev/gdm-create-vaporware-emulator/node_modules/svelte-adapter-appengine/node_modules/esbuild/lib/main.js:680:7)
    at Socket.emit (node:events:519:28)
    at addChunk (node:internal/streams/readable:559:12)
@dglazkov
Copy link
Collaborator

ESM most definitely support top-level awaits! This error is typically due to some misconfiguration. I've seen it happen before, and the fix is usually messing with esbuild/rollup config files.

@dglazkov
Copy link
Collaborator

dglazkov commented Jun 24, 2024

The underlying issue is that the the bundler/compiler used by svelte-adapter-appengine is too pessimistic about the target on which this code is going to run, so it bails.

AppEngine supports node22 now, which most definitely supports top-level awaits.

For example, here's the fix for this problem for esbuild/vite:

optimizeDeps: { esbuildOptions: { target: "esnext" } },
build: {

@j3ffgray
Copy link
Author

Hmmm, yeah, updating the vite.config.ts was the first thing I tried. I also tried manually setting the version of Node in the svelte config (for the app engine adapter), but I'm still receiving the build errors.

I'll keep poking around, thanks for the quick response!

@dglazkov
Copy link
Collaborator

One thing that might help -- I see that you're targeting CommonJS (cjs) -- why not use ESM?

@j3ffgray
Copy link
Author

I'm not attempting to target cjs, and I'm no wizard at Vite or Rollup, mostly just using the App Engine adapter Halfdanj created and using its stock configuration.

Any tips would be appreciated, but in the meantime, I've swapped Sveltekit adapters in favor of sveltekit-static, which has its own issues, but allowed me to deploy something to App engine that can run for the time being.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants