Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

top-level await not possible #1178

Open
acarl005 opened this issue Aug 15, 2022 · 8 comments
Open

top-level await not possible #1178

acarl005 opened this issue Aug 15, 2022 · 8 comments
Labels
enhancement New feature or request

Comments

@acarl005
Copy link

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.

@danez
Copy link
Contributor

danez commented Aug 18, 2022

Unfortunately, this is currently not possible as esbuild does not have support for transpiling top level awaits, so it cannot be used in commonjs modules, which zip-it-and-ship-it produces.

We are still experimenting with ES modules and how we best want to integrate this into the whole flow. I will use your request for TLA in our discussions.

@danez danez added the enhancement New feature or request label Aug 18, 2022
@ekwoka
Copy link

ekwoka commented Apr 5, 2023

which zip-it-and-ship-it produces

It really just shouldn't though.

It really shouldn't have ever relied on CJS. 2019 was already too late to be making new things with CJS.

Would be nice to get this fixed.

@eduardoboucas
Copy link
Member

It really shouldn't have ever relied on CJS. 2019 was already too late to be making new things with CJS.

Unfortunately we didn't have another option back then, because the underlying serverless provider (AWS Lambda) hasn't added support for ESM until January 2022.

This is definitely on our radar, though, and we're actively working on ways to fix this.

@ekwoka
Copy link

ekwoka commented Apr 10, 2023

Unfortunately we didn't have another option back then, because the underlying serverless provider (AWS Lambda) hasn't added support for ESM until January 2022.

😢 That is surprising they were so late.

Also didn't realize Node was so far behind the browsers implementing this.

@user72356
Copy link

News on this?

@james2doyle
Copy link

I'm hitting this error with an Astro build using Netlify. The weird thing is that my files are .mjs not .cjs

✘ [ERROR] Top-level await is currently not supported with the "cjs" output format
10:12:15 AM:     .netlify/functions-internal/chunks/pages/_...page_.astro.57cc126c.mjs:377:11:
10:12:15 AM:       377 │ } : await (await fetch(endpoint$1, {

The error seems nonsensical giving the extension on the file...

@ekwoka
Copy link

ekwoka commented May 25, 2023

@james2doyle it was mentioned above, but all the Js/TS files are transpiled to cjs. When this product was being made, AWS did not have any support for ESM. It does now, but it's far more recent than you'd expect.

Your filenames don't matter in that regard, just like how your functions can be TS, but they still become cjs.

@nichoth
Copy link

nichoth commented Jan 4, 2024

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants