Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.readFileSync cannot find file when deploying with lambda

In my code I am calling a query from my lambda function

let featured_json_data = JSON.parse(fs.readFileSync('data/jsons/featured.json'))

This works locally because my featured.json is in the directory that I am reading from. However when I deploy with serverless, the zip it generates doesn't have those files, I get a

ENOENT: no such file directory, open...

I tried packaging by adding

package: 
include: 
 - data/jsons/featured.json

but it just doesn't work. The only way I get this to work is manually adding the json file and then change my complied handler.js code to read from the json file in the root directory.

In this screenshot I have to add the jsons then manually upload it again and in the compiled handler.js code change the directory to exclude the data/jsons

enter image description here

I want to actually handle this in my servereless.yml

like image 845
kareem Avatar asked Sep 11 '25 20:09

kareem


1 Answers

You can load JSON files using require().

const featured_json_data = require('./featured.json')

Or better yet, convert your JSON into JS!

like image 165
Noel Llevares Avatar answered Sep 13 '25 10:09

Noel Llevares