Referenced files in deployed functions

Hey all,

I am building a Gatsby site with Netlify function that share some code/files together. I have managed to import/require some code from Gatsby folders into the Netlify function and locally the function is working correctly and actually including a referenced YAML file. However, when deployed, this referenced YAML file cannot be located (Error: ENOENT: no such file or directory).

I assume this is related to Referencing files in functions, but unfortunately I still do not quite understand Profile - Dennis - Netlify Support Forums 's answer. Could anyone confirm that this is a similar case and if so, further explain their answer (ideally in relation to my code/folder structure), please?

Resources:

I was also wondering if this was working locally only by coincidence that the depth of subfolders in relation to root folder (source .netlify/functions built into dist/server) was the same (and therefore the relative path in the import was the same), but turns out it still works locally even if the depth is not same (i.e. .netlify/functions built into dist/server/another-subfolder). Unfortunately, still no progress with the deployed function.

I have finally managed to get this working, using a combination of bundled and zipped function approches, inspired by GitHub - netlify/function-deploy-test - ultimately understanding the linked topic.

The steps are following:

  1. Add conditional path to referenced files based on where the Netlify function is run (as in function-deploy-test/zipped-function.js at master · netlify/function-deploy-test · GitHub)
  2. Bundle the JS function using the regular netlify-lambda build command
  3. Copy the referenced files to the directory of bundled function from previous step
  4. Zip all contents of this directory
  5. Delete all files/folders except the zipped file

Here are all my modifications in relation to resources in the initial post:

Personally, I prefer this over the raw zipped function approach for this use case, as there is no additional package.json or other stuff required. Hopefully this will help someone tackling the similar issue!

1 Like

thanks for sharing this!! this is so useful for other people!