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 directorynnnTROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.mdn",
"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
Answers
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.
I finally managed to make it worked and described the solution here, hope it’s helpful: https://konarskis.substack.com/p/puppeteer-aws-lambda
The key was to match the versions of puppeteer and chromium, and download the chromium binary at runtime as opposed to trying to put it inside the Lambda itself. This way, we don’t need any layers, external dependencies, or other configuration changes.