netlify / zip-it-and-ship-it

Intelligently prepare Node.js Lambda functions for deployment

Home Page:https://www.npmjs.com/package/@netlify/zip-it-and-ship-it

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

top-level await not possible

acarl005 opened this issue · comments

commented

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.

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.

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.

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.

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.

News on this?

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...

@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.

+1