Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I have a very simple node lambda function which reads the contents of the packaged file in it. I upload the code as a zip file. The directory structure is as follows.

index.js

readme.txt

Then have in my index.js file:

fs.readFile('/var/task/readme.txt', function (err, data) {

if (err) throw err;

});

I keep getting the following error NOENT: no such file or directory, open '/var/task/readme.txt'. 

I tried ./readme.txt also.

What am I missing?

1 Answer

0 votes
by (44.4k points)

This works:

'use strict'

 

let fs = require("fs");

let path = require("path");

 

exports.handler = (event, context, callback) => {

        // To debug your problem

        console.log(path.resolve("./readme.txt"));

 

        // Solution is to use absolute path using `__dirname`

        fs.readFile(__dirname +'/readme.txt', function (err, data) {

            if (err) throw err;

        });

};

Add this below console.log in your answer to debug your code.

console.log(path.resolve("./readme.txt"));

Lambda node process could be running from another folder and looking for a readme.txt file from it as you have given the relative path. Providing an Absolute path will solve this.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...