I have a bunch of Netlify functions in the functions folder defined in netlify.toml.
They aren’t in subfolders, just individual files for each endpoint. There is one node_modules folder in the project root.
I am deploying via netlify-cli and it’s working well.
Now I’d like to have some shared code between the functions, utility functions etc.I noticed in the docs there are some netlify.toml settings for including files and packages, but it’s unclear to me how these settings work.
How can I include these utility functions so each endpoint can use them?
Thanks for the reply. I am happy to try es6 modules, I guess that quite straight forward.
What I don’t understand in the thread example is that surely creating a ‘shared’ folder in the functions directly will create an unwanted endpoint, won’t it?
“The function endpoint is determined by its filename or the name of its dedicated parent directory”
Based in the results from your tests, seems like it isn’t the folder name that determines the function name otherwise you would have gotten some server errors rather than 404s when doing an http request on the shared folder as an endpoint.
The function I was trying to access in the “shared” directory isn’t a function endpoint because it is outside the functions directory (this is my understanding.) As it also says in the docs functions are built. I do not know the complete technical process behind this, but as with most code when it is built, all source files are put into a single executable.
When you publish a JavaScript function, our build system does some processing to package your code and its dependencies into a self-contained, deployable artifact. This is known as bundling .
I got this working, it’s as you describe. Also you don’t even have to use ES modules as someone else mentioned earlier in the thread. I got it working with regular Node modules.
I think it looks at the name of the export and only creates an endpoint if it’s called ‘handler’. I thought it looked at the folder name.
It’s nice to have an easy way to share common code
For anyone else stumbling upon this issue, the specific part of the Modern, faster Netlify Functions announcement that I needed to add to my project was the configuration below in my netlify.toml file: