Functions not being recognized

I’ve played with Netlify Functions quite a bit, but I swear I always screw this up somehow.

I’ve got an existing site tied to GitHub. It was NOT using functions.
I added a netlify.toml to the root:

[build]
	functions = ".functions/"

I used the CLI to scaffold a helloworld function and named it alexa1. I’ve now got .functions/alexa1/alexa1.js.

I push to GitHub, confirmed that Netlify auto-built, but my function does not deploy. According to the docs (Functions overview | Netlify Docs) I only need to specify the folder and use CD, which I am, so what am I missing here?

As just a comment to the above, I can run netlify dev and can absolutely hit my function. So it works in dev, not in production.

More information:

a) On a whim I added a package.json, didn’t help.
b) I added netlify-lambda as a dependency, didn’t help.
c) I’ve tried different variations of the path setting, so for example, ./.functions or ./.functions/ and it doesn’t seem to help either. My assumption is that if Netlify recognizes that I’ve specified a functions in my toml (and it did) but it could not read or see the folder, it would surely throw a warning/error of some sort.

I was having the same issue during the weekend.

My problem was that in my package.json file I wasn’t calling any functions to actually have the functions folder built during the build process.

What solved it was this line on package.json

"build:lambda": "netlify-lambda build backend",

backend is the folder that I have all my files that are setup for netlify lambda functions, so my tree structure looks like this.

root
| backend
| | servers.js
| src
| public
| package.json

I also used the following package to group different scripts

"npm-run-all": "^4.1.5",

so this is what my scripts object looked in package.json. Note that I am also starting a local server with pm2. Something I noticed was a thing on this netlify repo GitHub - neverendingqs/netlify-express: Express.js hosted on Netlify

  "scripts": {
    "start": "run-p start:**",
    "build": "run-p build:**",
    "start:app": "react-scripts start && pm2 start server-local.js",
    "build:app": "react-scripts build",
    "build:lambda": "netlify-lambda build backend",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

Also, after everything is deployed, you’ll need to try the following url to check if your function is running

https://sitename.netlify.com/.netlify/functions/filename

or, if you’re routing multiple endpoints in the same file with something like express

https://sitename.netlify.com/.netlify/functions/filename/endpoint

I’m pretty new to this but hopefully some of this helps you :slight_smile:

1 Like

According to the docs though - nothing is required in package.json to enable function support. I’ve got other sites w/ functions that don’t need this. It’s just this particular site that is failing.

Is your lambda requiring something from node modules?

Nope. It isn’t. Sorry, need 20 characters to reply. :wink:

Hmm, let me play with my function sandbox.

Okay, so i can definitely reproduce this here:

https://cocky-joliot-f8ee6d.netlify.com/

@fimion you cant nest functions like that functions/nested/test.js - if your function must be in a folder, then it has to be functions/nested/nested.js. this is documented here Build functions | Netlify Docs

i made a repro of raymond’s described issues and could not replicate

raymond shared his repo: GitHub - sw-yx/NetlifyTestingZone-cfjedimatter

i can replicate. without build command the functions detection is skipped. make sure to always specify build command and publish dir, either in UI or netlify.toml. i try to encourage this heavily in all my demos

I can confirm that adding a build command (I just used #) worked.

Netlfy, please please please please consider this an official bug report to add this to the docs. I started with this issue about five days ago and have been stuck since then on what could be addressed in one sentence in the docs. (Well, a bolded sentence.) Please consider adding this.

2 Likes

hey @cfjedimaster - thanks for pointing this out. We’re making note of it, and I’ll touch base with docs for you.

Gosh, really, one “please” is plenty! :stuck_out_tongue:

We do say that functions are deployed “after running your build command”, but I don’t think we really considered a lack of build command, and the implication there isn’t particularly strong.

I’ll add an extra sentence there to say that you must actually have a build command. I hesitate to bold it because we already have bold in that paragraph (in response another person’s debugging frustration :wink:).

We also have plans to make bigger improvements to the Functions docs in the future, and we’re saving links to community posts like this to make sure we’re addressing them properly when we do.

Thank you! One more request though - please share the tip about using “#” for cases when you don’t have a need to build anything. To be honest if I specify a functions dir that really should be enough. Why the need for a bogus build command at all?

Heh, as a matter of fact, I did add that note about # to my PR.

I suspect that the reason the build command was required is because functions were originally built as part of the build system - which gets a bit short-circuited when there’s no build command. Now that there are other ways to build and deploy Functions, and with the new Build beta in progress, I suspect this may change in the coming months. I’ll make sure the docs stay up to date!

Thank you, thank you, thank you. :slight_smile:

:laughing:

You’re welcome, you’re welcome, you’re welcome! :wink:

The updated docs are now published:

This looks excellent, thank you!