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?
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.
Note: The script should have probably been called “lambda:zip” instead of “lambda:bundle”, as this might be confusing.
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!
If anyone is searching for a solution to this in 2022. You can now specify files or folders you want to include with the function in your netlify.toml file:
[functions]
# Directory with serverless functions, including background
# functions, to deploy. This is relative to the base directory
# if one has been set, or the root directory if
# a base hasn’t been set.
directory = "functions/"
# include all files in the functionsFolder
included_files = ["functions/functionsFolder/**"]