How to access a JSON from a lambda function while deployed

Hello, I have built a lamba function. It works locally. It has to import a json. When I deploy it with the same code to bitbucket, and build the site, when I call the function and go to the functions tab on netlify. it says file not found.

Error: ENOENT: no such file or directory, open ‘/var/task/lambda/connection.json’

I’m pretty positive the path is correct. How do I access a JSON from a lambda function? It doesn’t seem that the JSON actually gets included in the build and deploy, so it sits in bitbucket but never gets built. How do I include this? I dont need a database for something so simple right?

someone please help me, I’ve been stuck on this for days, almost a week. I just want to use a JSON in my lambda function. Do I need to now set up a data base and call that? Or can I just store it in a file next to my lambda function?

hmm, i am not a functions expert, but have you tried downloading your site to make sure what you think should be there is actually there?

i’ll ask our local functions pro to take a look at this when he can also.

you might additionally look here:

to see if there is an example of what you are trying to do!

Thanks! I downloaded the site, however, this is only the front-end. None of the lambda functions get downloaded. Do you know how I would do this? Im still stuck on this

Hi, @chrisleeharris. You are correct that the functions are not available in the deployment zip file.

It sounds like the require JSON file isn’t being bundled into the function archive so isn’t available post deployment.

Here is an example community topic where someone else is asking something similar:

One tool to prepare/bundle functions for deployment is the “zip-it-and-ship-it” tool:

Also, @marcus here at Netlify created the following repo as a demo of how this (bundling a JSON file to be read in by the function) can be done:

I’m hoping these examples provide additional insight into the how files, JSON or other types, are bundled with function code to be accessible by the function after deployment.

If there are other questions or if there is more we can do to assist, please let us know.

@luke Thanks! ive read through it all, but what are the actual commands I would use to zip this? Some places I see using netlify deploy, but does then I read you need to zip it and ship it before. What are the actual real steps for this? I dont really see anything on that

@luke ok this is driving me crazy. I came across a blog of a guy basically saying the same thing as me
https://www.freecodecamp.org/news/you-cant-get-there-from-here-how-netlify-lambda-and-firebase-led-me-to-a-serverless-dead-end/

What is going on? Why is it so difficult for me just to require files in my lambda functions? Is this how netlify lambda functions will be? I have been at this for days and if I didn’t use this Lambda function approach, I could have been finished days ago. Am I doing something wrong??

I got my first JSON to load, but now I am trying to get an id file to load next to it, ugh, the file is literally sitting next to it I have functions for this, but NOOOOOOO, lambda functions dont allow you to import files without a bunch of mumbo jumbo. How do you use zip-it-and-ship it I havent found a single doc where it shows how to actually implement it???

hi there, i am wondering if this thread is helpful?

Hi there. @marcus I am trying your example GitHub - mraerino/netlify-functions-readfile but i just got this error “error decoding lambda response: error decoding lambda response: json: cannot unmarshal object into Go struct field .body of type string” when running the function.
Thanks for your help.

Sorry to hear that! I just opened a PR to fix that if you wanna take a look and try this code instead: Use __dirname instead of process.env.LAMBDA_TASK_ROOT by kaganjd · Pull Request #1 · mraerino/netlify-functions-readfile · GitHub

I got a similar error.

My code
res = JSON.parse(await fs.readFile(resolve(__dirname, 'public', 'json/website.json'), 'utf-8')

Output:
ENOENT: no such file or directory, open '/var/task/functions/public/json/website.json'"

Deploy is https://658d4fc4635e159fc87ad35b--tifun.netlify.app/.netlify/functions/utils

Based on this: I have Problem with finding a json file - #28 by tik9, you’ve resolved the issue.