Netlify dev: function returns 404 when using functions/functionname/index.js syntax instead of functions/functionname.js

Hi there, I’m experiencing the following issue:

According to this documentation, we should be able to use an index.js file inside of a folder name to reach the endpoint.

E.g. /functions/hello-world.js should be equivalent to /functions/hello-world/index.js

This works fine in production (I’m using a custom build process via Gatsby Cloud, the onPostBuild hook and @netlify/zip-it-and-ship-it).

Examples:
https://gatsby-cloud-netlify-functions.netlify.app/.netlify/functions/test
https://gatsby-cloud-netlify-functions.netlify.app/.netlify/functions/hello-world

However, this does not work when utilizing netlify dev during local development with a corresponding netlify.toml file. When I attempt to hit the functions on localhost, I receive the following console errors:

GET http://localhost:8888/.netlify/functions/hello-world

Request from ::ffff:127.0.0.1: GET /.netlify/functions/hello-world
Response with status 200 in 9 ms.

GET http://localhost:8888/.netlify/functions/test

Request from ::ffff:127.0.0.1: GET /.netlify/functions/test
Response with status 404 in 0 ms.
Request from ::ffff:127.0.0.1: GET /.netlify/functions/test.html
Response with status 404 in 0 ms.
Request from ::ffff:127.0.0.1: GET /.netlify/functions/test.htm
Response with status 404 in 0 ms.
Request from ::ffff:127.0.0.1: GET /.netlify/functions/test/index.html
Response with status 404 in 0 ms.
Request from ::ffff:127.0.0.1: GET /.netlify/functions/test/index.htm
Response with status 404 in 0 ms.

Here’s my minimal repo configuration:

Base Repo: gatsby-starter-default

Folder Hierarchy:

.
|-- src
|   |-- functions
|   |   |-- hello-world.js
|   |   |-- test
|   |   |   |-- index.js

netlify.toml:

[build]
  functions="public/functions" #this is for gatsby cloud config

[dev]
  functions="src/functions"

Running gatsby with netlify dev and what should be all the appropriate ports.

Any advice would be appreciated.

Hey @ardiewen,
I responded and tagged you but realized I misspoke! I can confirm that this is the CLI behavior. When you use the functionName/index.js format, the CLI doesn’t find index.js given the functions directory that you set in the [dev] block.

Would you be willing to open a feature request on the CLI repo for parity with the Netlify platform on this? I know the CLI team would also love a PR if you were interested. I think you’d be working in here: cli/get-functions.js at 294818b430cd0e2a93e648b533cd83d0352853cf · netlify/cli · GitHub

You can see the whole flow by checking out cli/src/commands/functions/invoke.js. If you try netlify functions invoke:test in your example above, you’ll get this error:

because the isValidFn check fails. That fails because getFunctions fails:

It sounds like you have a workaround, but please let us know if there’s anything else we can help with.

Thanks @jen for looking into this.

I submitted a PR per your suggestion:

1 Like

Amazing!!! Thanks so much for your contribution here :trophy: