0

I want to read zip file from local in Node JS (Also I would be hosting this backend on Linux based server) In the below code, exampleLambda.zip file is in the same folder where the .js code file exists.

 const zipContents = fs.readFileSync('exampleLambda.zip');
    
    var params = {
        Code: { 
               // here at below I have to pass the zip file reading it from the S3 public bucket
               ZipFile: zipContents,
        },
        FunctionName: 'testFunction', /* required */
        Role: 'arn:aws:iam::149727569662:role/ROLE', /* required */
        Description: 'Created with tempalte',
        Handler: 'index.handler',
        MemorySize: 256,
        Publish: true,
        Runtime: 'nodejs12.x',
        Timeout: 15,
        TracingConfig: {
            Mode: "Active"
           }
      };
      lambda.createFunction(params, function(err, data) {
        if (err) console.log(err, err.stack); // an error occurred
        else     console.log(data);           // successful response
      });

while reading the file it throws error Error: ENOENT: no such file or directory, open 'exampleLambda.zip error I have also tried with

const zipContents = fs.readFileSync('./exampleLambda.zip');

The file is in the same folder where the .Js file exists.

Why this throws an error, I am using fs lib from nodeJS. Is there any way around for this? Also, I want a solution where It also works on the server as well which is a Linux base because I am gonna upload it there.

August Dev
  • 21
  • 6
  • 1. If this is regular operation & is not part of a deployment pipeline - consider that maybe a regular unzipping of a file is not such a good idea 2. Please post the output of ls from the dir where the command is running, perhaps even use the appropriate node fs command When I used readFileSync on my node scraper recently I did not have `./` in front of a file name in the current directory – Scott Anderson Apr 05 '21 at 18:29
  • @ScottAnderson I have no other option left for that, As I have to pass the template file to create the AWS lambda function programmatically. Another option is the AWS bucket but it restricts the user and I want to keep it generic for all. – August Dev Apr 05 '21 at 18:32
  • @ScottAnderson I tried that ```./``` and without. but didn't work for me – August Dev Apr 05 '21 at 18:33
  • Ok fair enough can't comment on AWS workflows I'm afraid. Can you please try and output fs.readdir as demoed here: https://stackoverflow.com/questions/2727167/how-do-you-get-a-list-of-the-names-of-all-files-present-in-a-directory-in-node-j – Scott Anderson Apr 05 '21 at 18:35
  • @ScottAnderson Just a NOTE - I am doing an API call to create an Lambda on AWS. – August Dev Apr 05 '21 at 18:37
  • Yeah I don't know what this is, can you output readDir at all, or is it not permitted? – Scott Anderson Apr 05 '21 at 19:00
  • While the zip file may or may not be in the same folder as your source .js file, it's clearly not in the same folder that your Node application is being run from. Where are you running this code? Print out `process.cwd()`. – jarmod Apr 06 '21 at 01:00

0 Answers0