Function works locally but 500 errors when I deploy on Netlify

I’m testing Netlify functions and am facing an issue where functions will run locally will return 500 errors on Netlify.

The “website” I have consists of a single function (available as a git repo), with the following directory structure:

.
└── netlify
    └── functions
        └── hello.js

The hello.js is a function that gives you a random number every time you call it:

exports.handler = async (event, context) => {
  return {
    statusCode: 200,
    body: JSON.stringify({ number: Math.round(Math.random() * 4000) })
  }
}

When I run it locally using netlify dev, the function works as expected:

curl -i http://localhost:8888/.netlify/functions/hello
HTTP/1.1 200 OK
x-powered-by: Express
date: Fri, 14 May 2021 12:50:12 GMT
connection: close
transfer-encoding: chunked

{"number":519}

But when I deploy using netlify deploy and request the function URL, I get a 500 error:

curl -i https://609e6fcd432293c297e2ebc9--kind-mcclintock-8e20b3.netlify.app/.netlify/functions/hello
HTTP/2 500 
date: Fri, 14 May 2021 12:41:13 GMT
age: 0
x-nf-request-id: faca3d10-c5df-4da3-b3af-f0e31b0ab8f3
content-length: 0
server: Netlify

What am I doing wrong? Some pointers would be helpful.

1 Like

I’m not sure what’s wrong. I just deployed your code and got it working:

https://compassionate-bhaskara-d97982.netlify.app/.netlify/functions/hello/

Here’s the repo: GitHub - Hrishikesh-K/laughing-octo-sniffle

Maybe the name of the folder is making a difference?

As per Configure and deploy Functions | Netlify Docs, the directory name should be netlify/functions unless you override it with a netlify.toml file.

Regardless, I moved the functions directory from netlify/functions to functions and I redeployed, but it gives me a 404 error: https://609e7cf861eaece05a111c84--kind-mcclintock-8e20b3.netlify.app/.netlify/functions/hello

In your case, the URL has a “/” at the end. I tried adding a “/” to mine and it’s still a 404.

Hi @hrishikesh and @supriyo-biswas

This is a one of several posts I have seen recently regarding 500 Errors on functions deploys (see also Netlify functions returning 500 errors and Site deploy with functions failing with JSONHTTPError: 500 Internal Server Error and Deployed functions return 500 and possibly others Search results for '500' - Netlify Support Forums).

As per a previous post of mine (Netlify functions returning 500 errors - #3 by coelmay) I was able to successfully deploy functions using a GitHub repo, but deploying them directly via netlify-cli resulted in a 500 error served (even when using the Netlify Playground “Hello World” function).

There is something amiss here. Either we are all doing something wrong, or the netlify-cli deploy isn’t doing something right.

Hoping a Support Engineer can put in 2c here.

I’m not sure when this changed (or was it always like this?). I was aware about netlify/functions folder but just functions folder worked fine without specifying anything too. That’s how I have always used it in my projects, like you can see I don’t have any .toml file in the repo I linked above.

Just to check, does specifying a Functions directory make a difference? Does the function show up in your dashboard? If not, would you try creating another website? Another thing you can try is, instead of using CLI, see if deploying from GitHub makes a difference?

Honestly, this is a really surprising error.

EDIT: Deploy in seconds with Netlify CLI | Netlify also mentions some words of caution and recommends using CI instead of CLI whenever possible, but I’m not sure if it relates to this thread.

When I move my function directory from netlify/functions to functions, the number of functions in my dashboard goes to 0.

Deploying using the “Create site from Git” option in the Netlify dashboard works, regardless I’d like to know why it wouldn’t deploy. My git repo is linked in the original post.

Just like @coelmay mentioned, this problem has been coming up a lot lately that functions deployed via CLI are returning 500 errors. Till this problem gets identified/solved, I think you’d have to deploy using CI.

Hey there folks! Thanks for bringing this to our attention. I have shared this with the Support team to look into further.

2 Likes

The same is happening for me. Deploys via netlify-cli show no error, but the functions always return 500, and the logs stay empty. Deploying via the web interface works.

2 Likes

I’m also encountering this issue. Deploying via the Git integration / Web UI works, but deploying via netlify-cli doesn’t (it succeeds, but functions are not working). HTTP error 500 from all functions and no logs at all.

Tried using older netlify-cli versions back to 3.8.* but getting the same error.

1 Like

Thanks, all, for the reports- and especially to @coelmay to collecting other reports of this issue across the forum. I’ve dug into our logs and am seeing an error related to file unzipping. I’ve escalated to our CLI team and will report back here when we know more!

2 Likes

Hi all,
For the time being, could you try downgrading CLI to v3.28.1? Please let us know how that goes, and we’ll continue to keep you posted.

It would also help us debug further if you can share:

  • the CLI version and environment they’re on ( npx envinfo --system --binaries --npmPackages netlify-cli --npmGlobalPackages netlify-cli )
  • the exact command they’re running
  • the contents of their netlify.toml
  • a repo from which we can reproduce, if at all possible

Thanks!

Hi @jen
Thank you the shoutout. Before I downgrade the CLI and test I shall provide the requested data.

npx envinfo --system --binaries --npmPackages netlify-cli --npmGlobalPackages netlify-cli

  System:
    OS: macOS 11.3.1
    CPU: (12) x64 Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
    Memory: 72.95 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.1.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.11.2 - /usr/local/bin/npm
  npmGlobalPackages:
    netlify-cli: 3.29.14

Commands usually run as follows

ntl init 
ntl dev  # check that things are working
ntl deploy # preview deploy is broken, so no need to run `ntl deploy --prod`

netlify.toml reads

[build]
    functions = "functions"
    publish = "public"

[dev]
    functions = "functions"
    publish = "public"

A repo I used for testing is GitHub - coelmay/netlify-github-functions which contains the basic hello function. This is successfully deployed from GitHub at https://coel-functions-001.netlify.app/ but returns 500 when copied and deployed via CLI at https://coel-functions-002.netlify.app/. (Note: I copied the files locally, removing .netlify and .git files/directories before running ntl init.)

Hope this helps.

Hi again @jen

Downgraded to nelify-cli@3.28.1

npx envinfo --system --binaries --npmPackages netlify-cli --npmGlobalPackages netlify-cli

  System:
    OS: macOS 11.3.1
    CPU: (12) x64 Intel(R) Core(TM) i5-10600 CPU @ 3.30GHz
    Memory: 752.02 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 16.1.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.11.2 - /usr/local/bin/npm
  npmGlobalPackages:
    netlify-cli: 3.28.1

Local repo copy I ran

ntl init
ntl dev # hello function working
ntl deploy
ntl deploy --prod

Live: https://amazing-ritchie-9bbc7b.netlify.app/ which returns 500 (the post version is a rewrite to another site I was testing which returns 500 though it works when not deployed via CLI.)

1 Like

Thanks so much! Our CLI team have rolled out a fix, which you can get by downloading the latest version: 3.29.15. Could you please install, use the CLI to redeploy your sites with functions, and confirm that your functions are now working as expected?

3 Likes

Yay :partying_face: :netliconfetti:

All working as expected with 3.29.15.

2 Likes

I should also add thanks to @hillary, @jen, and the rest of Netlify team for getting this issue sorted out. ~5h ago it was escalated to CLI team, and the fix is deployed and working. If only all platforms could have such efficiency.

2 Likes

Tested it with 3.29.15 and it works :partying_face:

Thanks @hillary and @jen (and the CLI team ofc) for fixing this so quickly!

2 Likes

Hi all!

I started this thread and then moved states a couple of days later and just followed the emails, happy to see folks on top of it!

I’ve finally got my new living situation set up, and just upgraded from an older version of netlify-cli (3.15.0) up to 3.29.15 (and also tried 3.32.1, which appears to be the latest), and I’m still running into the same issue where ntl dev works perfectly, but ntl build and then ntl dev returns 500 errors (SyntaxError: Invalid or unexpected token).

Here’s my environment readout:

System:
OS: macOS 11.2.3
CPU: (12) x64 Intel(R) Core™ i7-8850H CPU @ 2.60GHz
Memory: 712.02 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.1.0 - ~/.nvm/versions/node/v16.1.0/bin/node
Yarn: 1.22.10 - ~/Dropbox/projects/strawberry-milk-photos/node_modules/.bin/yarn
npm: 7.11.2 - ~/.nvm/versions/node/v16.1.0/bin/npm
npmPackages:
netlify-cli: =3.29.15 => 3.29.15
npmGlobalPackages:
netlify-cli: 3.29.15

Is there something else I can be doing to reset things, or something I might have missed? Thanks in advance!

Hi @abethcrane,

You are getting the SyntaxError: Invalid or unexpected token when calling a function?

Are you able to post the rest of the error message? What file(s) does the error get triggered on? Are you able to post a code snippet?

1 Like