fs.readFileSync cannot find file when deploying with lambda

aws lambda read local file
how to read a file in lambda nodejs
fs read lambda
lambda node read file
nodejs read file
lambda require local file
serverless fs readfile
aws lambda file system

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

I want to actually handle this in my servereless.yml

You can load JSON files using require().

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

Or better yet, convert your JSON into JS!

fs.readFileSync cannot find file when deploying with lambda, fs.readFileSync cannot find file when deploying with lambda Check the current directory and check target directory content in deploy environment. 1 fs.readFileSync cannot find file when deploying with lambda Apr 10 '19 1 CakePHP asking for model table despite useTable = false Dec 3 '11 View all questions and answers →

I recommend looking at copy-webpack-plugin: https://github.com/webpack-contrib/copy-webpack-plugin

You can use it to package other files to include with your Lambda deployment.

In my project, I had a bunch of files in a /templates directory. In webpack.config.js to package up these templates, for me it looks like:

const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
  plugins: [
    new CopyWebpackPlugin([
      './templates/*'
    ])
  ]
};

Hosting a file along with my function - Support, I can't find any examples of doing a similar thing so is this just not possible? With the readFileSync (fs.js:551:33)","e.handler (/var/task/hello.js:1:146237)"]} Actually, that's only when serving locally - the file still cannot be read once deployed. The example does not do a webpack functions build using netlify- lambda . I went into the console, exported the function I had uploaded. And yes. The lambda.js file was still there.. I looked at the zip I created. The other issue is the lambda.jsfile may end up inside

For working with non-JSON files, I found that process.cwd() works for me in most cases. For example:

const fs = require('fs');
const path = require('path');

export default async (event, context, callback) => {
  try {
    console.log('cwd path', process.cwd());

    const html = fs.readFileSync(
      path.resolve(process.cwd(), './html/index.html'),
      'utf-8'
    );

    const response = {
      statusCode: 200,
      headers: {
        'Content-Type': 'text/html'
      },
      body: html
    };

    callback(null, response);
  } catch (err) {
    console.log(err);
  }
};

Deployment error if file does not have group read permission � Issue , Deployment error if file does not have group read permission #6613. Open build fails or AWS Lambda cannot run ( am not sure which) if any source file does not have group read permission set. What stacktrace or error message from your provider did you see? readFileSync (fs.js:551:33) at Object. This error is usually caused when your deployment package doesn't have the correct folder structure for the Lambda service to load your modules and libraries, or it has incorrect file permissions. (Lambda requires global read permissions.)

fs.readFileSync cannot find file when deploying with lambda

Check the current directory and check target directory content in deploy environment. Add appropriate code for that checking to your program/script.

Troubleshoot Lambda Deployment Package Upload Errors, How do I troubleshoot Lambda "permission denied" or "unable to import module" errors when I uploaded my AWS Lambda deployment package file and got an error. deployment package .zip or .jar file are incorrect, you get an error when you try to upload the file. readFileSync (fs.js:509:33)", "Object. The technical definition for AWS Lambda would be a Function as a Service. You deploy some code, it gets invoked, processes some input, and returns a value. Simple! Hold up a sec. We need to mention a crucial fact. All lambda functions are stateless, meaning they cannot store persistent data.

json, 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 = This is even more confusing in say the case of @types/aws-lambda. The aws-lambda npm package is a command line tool for deploying to lambda, but @types/aws-lambda is a set of typings specifically for the JS APIs provided by running code on lambda.

netlify/lambda-functions, Is it possible to access static files from a lambda function? e.g. fs.readFileSync( path.resolve('foo.jpg')) the netlify-lambda step to the build, and it works fine locally, but I get It works fine locally using netlify-lambda but once deployed to Netlify it doesn't work (all my other functions in the project work fine). Learn how to easily deploy your Node.JS applications to the AWS Lambda function-as-a-service. How to Deploy JavaScript & Node.js Applications to AWS Lambda - Twilio Level up your Twilio API skills in TwilioQuest , an educational game for Mac, Windows, and Linux.

How to publish and use AWS Lambda Layers with the Serverless , And when serving locally using netlify-lambda , I get this error: Function trying to use netlify to deploy that combination, lambda functions doesn't seem to be a static files from a lambda function? e.g. fs.readFileSync(path.resolve('foo.jpg')). These layers are added to your function’s zip file when published. In a way, they are comparable to EC2 AMIs, but for functions. The killer feature of Lambda's Layers is that they can be shared between Lambda functions, accounts, and even publicly! There are two aspects to using Lambda Layers: Publishing a layer that can be used by other

Comments
  • thanks, let me test. Just to clarify, do i have to specify the path of the json file or will require know where to find it?
  • require requires you to provide the relative path to the JSON file in the same way you require JS files.
  • Thanks so much it worked! Didn't realize thats how the rest of my javascript files are being found and could apply it to json files.
  • Can you look at my updated screenshot. The problem is when I deploy with servereless it doesn't include my directories, it compresses all the js files in handler.js and since my json files are static i have to manually add them and zip them back. I want to be able to include them in my deployment.
  • Thanks will add you!