Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.



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.3k points)

This works:

'use strict'


let fs = require("fs");

let path = require("path");


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

        // To debug your problem



        // 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.


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.

Browse Categories