Puppeteer in AWS Lambda - Missing libnss3.so Library

402 Views Asked by At

Issue with Puppeteer in AWS Lambda - Missing libnss3.so Library

I am trying to run Puppeteer on AWS Lambda using the chrome-aws-lambda package. However, I'm encountering an error related to a missing shared library, libnss3.so. The relevant part of the error message is as follows:

{
  "errorType": "Error",
  "errorMessage": "Failed to launch the browser process!\n/tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory\n\n\nTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md\n",
  "trace": [
    "Error: Failed to launch the browser process!",
    "/tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory",
    "",
    "",
    "TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md",
    "",
    "    at onClose (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:197:20)",
    "    at Interface.<anonymous> (/var/task/node_modules/puppeteer-core/lib/cjs/puppeteer/node/BrowserRunner.js:187:68)",
    "    at Interface.emit (node:events:526:35)",
    "    at Interface.close (node:internal/readline/interface:527:10)",
    "    at Socket.onend (node:internal/readline/interface:253:10)",
    "    at Socket.emit (node:events:526:35)",
    "    at endReadableNT (node:internal/streams/readable:1589:12)",
    "    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)"
  ]
}

This occurs when trying to launch Chromium using Puppeteer. My Lambda function is set up in Node.js and involves processing PDFs using Puppeteer. I suspect the issue is due to the Lambda environment missing the necessary libraries for Chromium.

Here's a snippet of my Lambda function where the error occurs:

const browser = await chromium.puppeteer.launch({
  args: chromium.args,
  defaultViewport: chromium.defaultViewport,
  executablePath: await chromium.executablePath,
  headless: chromium.headless,
  ignoreHTTPSErrors: true,
});

I haven't defined a Lambda layer for additional binaries or libraries. Is this the cause of the issue? If so, how should I go about setting up a Lambda layer to include the required libraries for Puppeteer?

Any help or guidance would be greatly appreciated.

2

There are 2 best solutions below

0
Shawn On

If the library you want to use it not already one of the pre-installed libraries (if it wasn't found, then it isn't), you can add it. This documentation gives the details, specifically on how to include other libraries.

I recommend using a layer for 3rd party dependencies. Just be sure to match the required structure of directories to ensure your dependency code will be found.

0
LONG On

I have the same issue now, seems like a node version problem. You can also habe a check here. Since node 14 is not supported anymore, perhaps you need to use the workaround with 'sparticuz/chromium'. I'm going to try it.