How to deploy a directory along with a function?

Is it possible to add a directory beside a function? I see when deploying netlify only adds the directories and files that you actually require / import. But I want to know if I am able to just have a directory there that is not actually imported but is read using readDir and readFileSync

1 Like

Hi @devotox!

Can you tell me more about your functions setup? Are you using netlify-lambda to bundle your functions or using the built-in zip-it-ship-it support?

One way to get an artbitrary file is to pre-zip your lambda function and put that zip file in your function folder. You should be able to access them kinda like this:

const fs = require("fs");
const path = require("path");

fs.readFile(path.join(__dirname, "other-file"), callback)

I am using zip-it-and-ship-it and i found that using readFile is not enough for that only requires seem to work.

Hi @devotox,

I think you’re right and zisi doesn’t check for fs functions to determine whether or not to include a file or directory with a function zip file. I recommend that you zip the function yourself. Also note that zip-it-and-ship-it is an open source project and you might want to create an issue or if you’re so inclined, put in a PR for this:

So it seems according to here if you have the folder as a subdirectory it should just work

I created a working example to allow you to discover where your setup might not work:

Hope this helps you discover what is wrong about your setup.
Feel free to ask if anything about that setup is unclear.

Thank you @marcus that definitely did help and I now got it almost working. So I think my issue was that I needed to use a bundled/folder style function, not just a singular JS file. This brings up another issue now. I had a utils directory with common code and configuration for both serverless functions and now this is not accessible since the function cannot see outside its own directory. Do you know what the best way would be to have a common directory without having to have it twice?

@devotox you can still use netlify-lambda to bundle your script, just make sure the bundled script ends up in a subdirectory of the functions folder.
It is possible that you need to have a custom webpack config for that.

So i was able to get this working without netlify lambda by just duplicating the utils directory into both bundled functions at build time using a shell script. Not the most scalable way atm but alas it works.

thanks for sharing your workaround, @devotox. If you find a more scalable way please do let us know, it might help others for sure.