javascriptaws-lambdacsvtojson

AWS Lambda- Cannot find module 'csvtojson'


I am using AWS Lambda service to read the content from the CSV file uploaded on the S3 bucket and convert it to the JSON format. I got stuckup on the CSV to JSON phase.

    const AWS = require('aws-sdk');
    const csvtojson = require('csvtojson');
    const S3 = new AWS.S3();

    exports.handler = async (event,content,callback) => {
        console.log(' inside trigger ')
        const src_bkt = event.Records[0].s3.bucket.name;
        const src_key = event.Records[0].s3.object.key;
        
        try {
            const params = {Bucket: src_bkt, Key:src_key}
       
       // get csv file and create stream
            const stream = S3.getObject(params).createReadStream();
            // convert csv file (stream) to JSON format data
            const json = await csvtojson().fromStream(stream);
            console.log(json);
      }
      catch (err) {
          console.log(JSON.stringify(err))
        return {
          statusCode: err.statusCode || 400,
          body: err.message || JSON.stringify(err.message)
        }
      }
    };

Error Message: "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'csvtojson'",

I tried to convert the node module on the lambda to check any luck.


Solution

  • You need to package and deploy the lambda as a zip file. The file should contain the javascript file with the handler function as well as the node_modules directory with all the dependencies.

    Zip File structure.

    handler.js

    node_modules/

    ......................../csvtojson

    how to setup the project

    Reference:

    https://aws.amazon.com/premiumsupport/knowledge-center/lambda-deployment-package-nodejs/

    hope this helps.