We don't understand how the included_files [functions] parameter works

We have read this: How to Include Files in Netlify Serverless Functions

And this: File-based configuration | Netlify Docs

But we are not able to make it work.

Here is a simple example: GitHub - juanmiguelguerrero/test-included-files-function

We want “./src/data.json” to be accessible in our lambda function “test.js”.

In netlify-cli dev environment, we always get the same error: Could not resolve "./src/data.json"

Could someone modify our example to make it work correctly???

Thanks

Netlify functions can’t access files outside of its own folder. If you put the json file in your functions’ folder, it should work.

Thanks Tom,

I’m sorry but I’m not satisfied with your answer.

We already know that a function cannot access files outside of its folder…

Have you read the first attached article?

I copy literally:

One option is to tell our build system the paths of any additional files that should be included in the function deployment. This can be done in the netlify.toml configuration file using the included_files property of the functions block,

(extract from How to Include Files in Netlify Serverless Functions)

Reading this I understand that by setting the included_files parameter the system will copy the selected files to the function folder automatically in the function deployment.

I copy the end of the article (literally):

You can include any type of file in your serverless functions, even if you reference them using dynamic expressions. You can add these files manually in your configuration file...

We think the author (@eduardoboucas) explains it clearly. Modifying the netlify.toml configuration you can automate the adding of any file in the deploy of your function.

Right, just making sure you’re setting things up correctly…You’re trying to load the file as if it’s within the functions folder though, but it’s not. Try ../src/data.json instead of ./src/data.json

Also, your folder is named scr instead of src, so you’ll need to fix that, obviously.

Yes !!!

Now the example is corrected and running: https://sharp-lichterman-df15d5.netlify.app/

The folder name error is a real newbie’s mistake. We will go to hell for this. :scream: Thanks.

Taking the root directory as the main directory instead of the function folder is something you might want to include in the documentation. We had understood that everything happened inside the folder where the functions are hosted.

Thank you very much for your help.

This feature is very important for us because it helps us to simplify the deployment of on-demand builder functions. That’s great and simple !!!

Thanks Tom.

Taking the root directory as the main directory instead of the function folder is something you might want to include in the documentation.

Well… You’re trying to load the file from within the function, which lives inside the functions folder. So to reach the data file, you have to go back to root first, hence the ../.

If that’s the case, I’m screwed haha. Glad you’ve got it working though!

1 Like

Instead of using relative paths, I should be able to use absolute path which is described in the docs that @existo writes about:

[functions]
  included_files = ["languages/**"]

To access it in the function:
const res = fs.readfile('./languages/foo.json');

languages is in root folder I think. I should be able to add further dirs, as in ["languages/**", 'foo/**',..]

Hey there, @tik9 :wave:

Thanks for reaching out, and welcome to the Netlify Forums. Can you please share more about your post? Are you encountering obstacles? If so, please share your netlify site URL as well as your functions folder.

@tik9

We attach two screenshots that may be helpful…


2 Likes

Thanks for coming back and sharing those, @existo!!

@existo thanks for sharing, I will try it after I got solved another issue regarding build. I will create an issue but still do not know where to post a fresh one.

You can start a new topic anywhere, @tik9 :slight_smile: start here for instance: