TL;DR
Serverless functions may not run in netlify dev
mode without additional setup not currently specified in the feature tour docs.
Details
๐ Howdy, Netlify friends!
I just went through this repo in preparation for a chat with the Netlify dev team tomorrow and got a bit stuck with the Create Your First Serverless Function
section of the demo. Specifically, I was able to deploy and get a serverless function running in production but not locally (i.e. using netlify dev
on my machine).
Unless I'm missing something (definitely possible, given my rusty JS chops!), it seems like these instructions on the Netlify Functions
page may need a bit of tweaking:
Save it, then run netlify dev to test locally.
Check your work by clicking the button below!
When I create the function and run netlify dev
on my machine as instructed, then click Test Your Function
, I get the following error:
![image](https://user-images.githubusercontent.com/12027291/185821582-0b5937b1-fe89-4ba6-ac30-114963fe4271.png)
In the local server logs, I'm seeing:
โ Starting Netlify Dev with Astro
> @example/[email protected] dev
> astro dev
[19:29:41] [snowpack] Hint: run "snowpack init" to create a project config file. Using defaults...
[19:29:41] [snowpack] Ready!
[19:29:41] [snowpack] watching for file changes...
[dev server] Server started in 383ms.
[dev server] Local: http://localhost:3000/
[access] /.netlify/functions/hello-world
[access] /404
[access] Not Found: /.netlify/functions/hello-world
โ ผ Waiting for framework port 3000. This can be configured using the 'targetPort' property in the netlify.toml
curl
-ing the function endpoint directly doesn't seem to be working either:
$ curl localhost:3000/.netlify/functions/hello-world
Not Found%
I played around with different filenames and paths for a bit before deciding to deploy to production to see if I could break my site there. However, to my surprise, the exact same code worked in prod:
![image](https://user-images.githubusercontent.com/12027291/185821857-af3a1184-c403-4bc9-b546-254d6cc549a6.png)
Unlike in dev, in production, curl
-ing the function endpoint works as expected:
$ curl https://MY-SUBDOMAIN.netlify.app/.netlify/functions/hello-world
hello world!%
In case it helps, here's a screenshot of the function I wrote (simple copy-pasta from the instructions) as well as its location within my project:
![image](https://user-images.githubusercontent.com/12027291/185822076-f3070145-c6df-475e-8fc5-728a6a14ab69.png)
I'm guessing that there must be some additional setup needed to spark the /.netlify
path magic and get functions running locally. It could be great if you could update the repo to include instructions on how to do that - or at least remove the suggestion to try running functions locally so anxious candidates like me don't tear more hair out than necessary ๐