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

I have a codebase that uses top-level await in a JS module. ESBuild seems to be forbidding me from using it due to the “cjs” output format.

Netlify site name: leafy-salamander-acbb48
Failing deploy: Netlify App
Source code: netlify-node16/index.js at master · acarl005/netlify-node16 · GitHub

Error:

1:04:06 PM: ────────────────────────────────────────────────────────────────
1:04:06 PM:   2. Functions bundling                                         
1:04:07 PM: Creating deploy upload records
1:04:06 PM: ────────────────────────────────────────────────────────────────
1:04:06 PM: ​
1:04:06 PM: Packaging Functions from .netlify/functions-internal directory:
1:04:07 PM: ---------------------------------------------------------------------
** WARNING **
The default Node.js runtime will change to Node v16 on 2022-07-11.
If you need to stay on v14, refer to https://ntl.fyi/pin-js-runtime.
---------------------------------------------------------------------
1:04:06 PM:  - render.js
1:04:06 PM: ​
1:04:06 PM: ✘ [ERROR] Top-level await is currently not supported with the "cjs" output format
1:04:06 PM:     .netlify/server/entries/endpoints/index.js:1:13:
1:04:06 PM:       1 │ const name = await Promise.resolve("world");
1:04:06 PM:         ╵              ~~~~~
1:04:07 PM: ​
1:04:07 PM: ❯ Failed to bundle functions with selected bundler (fallback used):
1:04:07 PM:    - render.zip
1:04:07 PM: ​
1:04:07 PM: (Functions bundling completed in 918ms)

A “fallback” function is used after the build fails, and the deploy is considered “successful”. However, the fallback function ends up crashing.

This one is not a Netlify issue, as far as I can see. SvelteKit appears to generate a CJS file:

and ESBuild doesn’t support that for top-level await:

You’ll have to work with either of those of support the other.

Sveltekit sometimes generates a cjs file, but not in my case.

It depends on the esm parameter of this function.

The value of that parameter depends on the node_bundler option in netlify.toml: kit/index.js at master · sveltejs/kit · GitHub

As you can see here, I am indeed using esbuild as my node_bundler, so Sveltekit is building mjs output.

Furthermore, when I look at the files themselves, the Sveltekit output is mjs

npm run build
cat .netlify/server/entries/endpoints/index.js
import "path";
const name = await Promise.resolve("world");
function get() {
  return {
    status: 200,
    body: {
      message: `Hello, ${name}!`
    }
  };
}
export {
  get
};

Hey there, @acarl005 :wave:

Thanks for your patience! We will share this with one of our relevant teams to see if they have additional information for you. Stay tuned to this thread!

:wave: @acarl005 , thanks for reporting this issue! Would you be comfortable opening an issue in the repo Issues · netlify/zip-it-and-ship-it · GitHub? If not, let us know and we can help get that filed for you. :slight_smile:

Issue filed: top-level await not possible · Issue #1178 · netlify/zip-it-and-ship-it · GitHub

1 Like