Netlify dev, functions & build - missing functions running locally?

Hi there,
So I have created a new CRA app, created a netlify.toml file with build settings in
[build]
command = “yarn build”
functions = “functions”
publish = “build”
and put my functions in a functions folder in the root. I am then attempting to call my function like this:

function handleClick(api) {
  console.log("api", api);
  fetch("/.netlify/functions/" + api)
    .then(response => response.json())
    .then(json => console.log("json", json))
    .catch(err => console.log("err", err));
}
// ...
<button onClick={() => handleClick("hello")}>Button 1</button>

but am getting the error:
App.js:7 GET http://localhost:8888/.netlify/functions/hello 404 (Not Found)
handleClick @ App.js:7
onClick @ App.js:29
/…
App.js:7 err SyntaxError: Unexpected token F in JSON at position 0

I have not deployed the site to netlify as yet but my understanding was that when I run netlify dev, this would take care of the creation of a .netlify/functions folder (which it has not). From my understanding this mocks up the serverless setup of netlify locally. Is this correct?

1 Like

Okay… so even though it felt as if I had tried this 100 times in different configurations, for some reason, as soon as I had posted this question the functions seemed to be firing properly so I will give an answer so that if anyone else is struggling then they can find this:
The reason my .netlify/functions folder was not being created is that I had to stop the netlify dev server then run netlify build. On doing this the .netlify folder was populated with a build & functions folder and it is this endpoint which can be hit from a fetch request inside my app.

2 Likes

Thanks so much for the follow-up with the answer! We very much appreciate you sharing it to help others in the community out :slight_smile: