Functions in subdirectory

Can anyone confirm that functions still can’t be placed in a subdirectory in 2023? Something like:

/netlify/functions/users/getUser.ts

…doesn’t work. At the present time it seems like the CLI doesn’t detect functions that are any deeper than the functions/ directory. So the example above would have to be:

/netlify/functions/getUser.ts

Coming from Vercel it’s quite inconvenient for projects that have a large amount of functions. I suppose what’s missing is a file based router for functions that maps an endpoint URL to a function path.

At any rate, I just wanted to confirm this. Thank you.

The documentation indicates that you can have subdirectories but that the directory is the endpoint name, and when using them the file deployed needs to be either named the same as the directory or index.

netlify/functions/hello.ts
netlify/functions/hello/hello.ts
netlify/functions/hello/index.ts

So what would be the advantage of this setup over having the function file directly under the functions/ directory? Is it so I can co-locate supporting source files within the same subdirectory as the function?

I have to dig into the documentation about this but, I thought I had read somewhere that it wasn’t recommended to co-locate other source files than the functions for fear of exposing uncompiled source code. But I may have read this somewhere else than Netlify. If you could please give me a recommendation on that… Thank you.

I’ve unfortunately got no idea.
I’ve only rarely used Netlify Functions, normally we work with Google Cloud Functions.

I believe that’s the case, based on this answer in another thread:

I can’t give you any recommendations on best practice way of working with it, since as mentioned I don’t work with it.

Other threads discussing function organization had suggestions of using any folder structure that you would like, and creating your own build script to move the files to the required Netlify location at build/deploy time.

The main takeaway though is that you can’t use the folder structure to organize your api paths, e.g.

/user/create
/user/delete
/user/update
1 Like

You can however use Fastify or Express to organise your routes:

Express: netlify-file-browser/api.js at v2 · Hrishikesh-K/netlify-file-browser · GitHub
Fastify: netlify-dns-verifier/api.ts at main · Hrishikesh-K/netlify-dns-verifier · GitHub