Shared modules for Netlify functions

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?

Hey there, @mjgs :wave:

Thanks for reaching out.

Can you try using ES6 import/export as described here: ES6 Modules and How to Use Import and Export in JavaScript | DigitalOcean and in this thread? Let us know if this works for you!

1 Like

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?

Hi @mjgs ,

I have created a shared directory under functions then used:

const a = require('./shared/this');
const b = require('./shared/that');

and this seems to work well. Navigating to /.netlify/functions/shared/function_name returns a 404 for me.

I have also tested putting the shared directory at the same level as functions thus changing the above too

const a = require('../shared/this');
const b = require('../shared/that');

This also works in my tests.

Thanks @coelmay - that’s awesome if it works.

I’m still slightly confused how the system determines which functions to setup as endpoints.

According to the docs:

“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.

Magic! :slight_smile:

In all seriousness…

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.

This might help Modern, faster Netlify Functions: New bundler and JavaScript features where is says:

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 .

1 Like

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 :slight_smile:

2 Likes

Great to hear you got it working @mjgs :partying_face:

1 Like

Magic in this case is not desirable imo.

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:

[functions]
  node_bundler = "esbuild"