Support Forums

Function returns 502 HTTP error

Hey there,

I am trying to deploy a function written in module syntax (import, export instead of require and module.exports) by utilizing node_bundler option set to esbuild in netlify.toml. Unfortunately after function being successfully deployed I am getting a 502 HTTP error response with text:
error decoding lambda response: invalid status code returned from lambda: 0

Function can be found here: netlify-functions-demo/nonsense.js at main · marovargovcik/netlify-functions-demo · GitHub

Of course my function was more complex but to understand what is causing I stripped all of it - here is the full version that causes the same behavior: netlify-functions-demo/nonsense.js at fe1bd939834d811a1bdcb6d4c87b2875ee5d3702 · marovargovcik/netlify-functions-demo · GitHub

Here is the netlify.toml: netlify-functions-demo/netlify.toml at main · marovargovcik/netlify-functions-demo · GitHub

URL to deployed version: https://netlify-functions-demo-v1.netlify.app/.netlify/functions/nonsense

Am I misusing node_bundler = "esbuild"? Or where am I making a mistake? I simply want to deploy function written in modern JavaScript (optional chaining, nullish coalescing operator). How do I do that? Deploying TypeScript function was a breeze but here it seems I cannot figure it out. A working example would be very welcome! Thanks.

Hi @marovargovcik,

You need to export the function as follows:

export async function handler() {
  return {
    body: '',
    statusCode: 204

I believe it’s not working because your function export is not async.

Hi, thanks for replying. I changed the function to exactly what you posted and I am met with an error about unexpected token export: https://netlify-functions-demo-v1.netlify.app/.netlify/functions/nonsense

Shouldn’t esbuild transpile and bundle function during deploy?

Also I quite don’t understand how doing

const handler = async () => { ... }
export { handler };

is different than

export async function handler() {}

I checked your repo at this stage:

const handler = () => {
  return {
    body: '',
    statusCode: 204,

export { handler };

So, it wasn’t using async. Otherwise, as you said, it’s not very different than the syntax you’ve now.

About the link you shared, that doesn’t seem to open for me.