Search code examples
pdfkit

pdfkit on JS/TS Lambda with CDK Error: ENOENT: no such file or directory, open '/var/task/data.trie


I'm trying to use pdfkit v.0.12.1 with AWS CDK and a Lambda function (tried both typescript and js). When I try to run it I get this error:

Error: ENOENT: no such file or directory, open '/var/task/data.trie The Lambda looks like this:

const PDFKIT = require("pdfkit")

const pdfPromise = new Promise(resolve => {
        const doc = new PDFKIT()
      
        doc.text('hello pdf Kit...Please work', 100, 50)
        doc.end()
      
        const buffers = []
        doc.on("data", buffers.push.bind(buffers))
        doc.on("end", () => {
          const pdfData = Buffer.concat(buffers)
          resolve(pdfData)
        })
      })

Lambda runtime is Nodejs_12_x All the code is importd into the Lambda, but I'm unsure if I have everything to run. Lambda file size is about 650kb. Any help appreciated on how to get around this.


Solution

  • pdfkit was not loaded properly in the lambda function. I solved this by following this advice for adding external dependencies to a lambda function in CDK.

    Basically, create a separate folder for your lambda in your CDK set up with a seperate node modules folder. On deploy, all of this will be packed together to have the dependencies available.

    After that, pdfkit works as expected.