Netlify functions: different return values between `netlify dev` and `serve -s build`

Very strange behavior. I have a lambda function that returns a JSON object. The call is simple. Here is the result from function:invoke

netlify functions:invoke comments --querystring "id=1"
◈ "port" flag was not specified. Attempting to connect to localhost:8888 by default
{"data":[{"user":{"avatarUrl":"https://avatars.githubusercontent.com/u/3497069?v=4","name":"JeffML"},"datePosted":"2022-10-24T22:47:50Z","isEdited":false,"isAuthor":true,"body":"First comment"}]}

Via netlify dev I can verify that within the application the call is correctly made and I get the expected JSON body returned, as above.

When I do a netlify build and then run the app in a static server via server -s build, the function call returns the index.html of the app. I can verify this by pasting in the function URL (http://localhost:40243/.netlify/functions/comments?id=1); this returns the same result as (http://localhost:40423), which is index.html.

Any insights?

Here’s the .toml settings:


[build]
  command = "react-scripts build"
  publish = "build"

[functions]
  directory = "netlify/functions"
  node_bundler = "esbuild"

Added notes:

  1. seems any URL based on localhost:40243 returns index.html
  2. netlify deploy works! So it is only a problem with the static server.

I see this in the .toml:

  ## Uncomment to use this redirect for Single Page Applications like create-react-app.
  ## Not needed for static site generators.
  #[[redirects]]
  #  from = "/*"
  #  to = "/index.html"
  #  status = 200

Though it’s commented out, this looks to be the problem with the static server. Maybe?

Hi @JeffML

Netlify functions only work with Netlify CLI (or when deployed on Netlify.)

1 Like