Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Failed to launch the browser process: error while loading shared libraries: libnss3.so: cannot open shared object file #164

Closed
pmakkar93 opened this issue Sep 30, 2020 · 34 comments
Labels
bug Something isn't working invalid This doesn't seem right

Comments

@pmakkar93
Copy link

pmakkar93 commented Sep 30, 2020

Environment

  • chrome-aws-lambda Version: 3.0.4
  • puppeteer / puppeteer-core Version: 3.0.4
  • OS: Mac
  • Node.js Version: 12.x
  • Lambda / GCF Runtime: nodejs12.x

Expected Behavior

Chrome should have load.

Current Behavior

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/master/docs/troubleshooting.md
at onClose (/var/task/node_modules/puppeteer-core/lib/Launcher.js:547:20)
at Interface. (/var/task/node_modules/puppeteer-core/lib/Launcher.js:537:65)
at Interface.emit (events.js:327:22)
at Interface.EventEmitter.emit (domain.js:483:12)
at Interface.close (readline.js:416:8)
at Socket.onend (readline.js:194:10)
at Socket.emit (events.js:327:22)
at Socket.EventEmitter.emit (domain.js:483:12)
at endReadableNT (_stream_readable.js:1220:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21)

Steps to Reproduce

const chromium = require('chrome-aws-lambda');
const puppeteer = require('puppeteer-core');

  async convertToPdf(request) {
    try {
      console.log('Starting puppeteer browser.');

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

      console.log('Puppeteer launched: ' + !!browser);


      if (request.sections.length > 75) {
        throw new Error('Only 75 report sections are allowed');
      }

      let pdfs = await Promise.all(_.map(request.sections, async (section, $index) => {
        if (section.s3Pdf) {
          let s3Response = await s3.getObject({
            Bucket: section.s3Pdf.bucket,
            Key: section.s3Pdf.key
          }).promise();

          console.log(`PDF ${$index} retrieved successfully`);
          return s3Response.Body;
        }
        const page = await browser.newPage();

        page.setViewport(request.viewPort || {
          width: 1024,
          height: 600
        });

        section.options = section.options || {};

        console.log(`Loading section ${$index}...`);
        await page.setContent(this.fromBase64(section.html), {waitUntil: ['networkidle0']});
        console.log(`Section ${$index} Load complete. Converting html to pdf using puppeteer. Options: ${JSON.stringify(section.options)}`);

        let body = await page.pdf(_.assign({format: 'letter'}, section.options));

        console.log(`PDF ${$index} created successfully`);
        page.close();
        return body;
      }));

Previously my code was running properly on nodejs8.10 supported AWS Lambda function but after forcefully upgrade to nodejs12.x, my code starts to throw this error. I tried upgrading chrome-aws-lambda and puppeteer-core version to 2.1.1 and even the latest 5.3.1 but no luck on resolving the issue.

@pmakkar93 pmakkar93 added the bug Something isn't working label Sep 30, 2020
@pmakkar93 pmakkar93 changed the title [BUG] [BUG] Failed to launch the browser process: error while loading shared libraries: libnss3.so: cannot open shared object file Sep 30, 2020
@alixaxel
Copy link
Owner

alixaxel commented Oct 1, 2020

@pmakkar93 With Node 12 the Lambda environment needs provisioning of additional shared libraries for Chromium to work, the earliest version that would work on this environment is precisely 3.0.4 (I advise you to upgrade to the latest 5.3.x though).

I think the issue that you're facing might be caused by a warm Lambda that is still keeping the files under /tmp and thus preventing these libraries from being provisioned. Please try a brand new deployment with 3.0.4 or any version since.


Edit: I just noticed that you're trying this on Mac? If so please check this wiki page.

@whoisjuan
Copy link

@alixaxel Any potential workaround for this? I also have this exact same problem but since 2.0.0, Puppeteer introduced a change that broke clipping beyond the viewport and this was needed to curve a lot of issues with the screenshotting mechanism.

@jmdiego
Copy link

jmdiego commented Oct 6, 2020

I'm getting this same issue with:

  • node 12
  • chrome-aws-lambda 5.3.1
  • puppeter-core 5.3.1

@yoshiokimoto
Copy link

It's not the exactly same case but I've seen same issue to initialize the browser object, missing libnss3.so. In my case it only happens when I run the lambda with aws-sam-cli in debug mode.
Actually I don't see the issue if I run the lambda at my local with normal mode (i.e. run with sam local invoke), but in debug mode (sam local invoke -d 5858) I can see the error message 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.

Actually I could notice that the environment variable AWS_EXECUTION_ENV is not defined in local lambda container if I run in debug mode, it's there in normal mode on the other hand.
And when I set that variable in template.yaml file (which contains the definition of serverless application in aws sam), then the error was gone.
My experience is only in aws sam, but hope this can be a clue to resolve someone's trouble.
My environment is:

  • Mac Catalina
  • chrome-aws-lambda: 5.3.1
  • puppeteer-core: 5.3.1
  • Node: 12.x

Also, I know there are few lines which use AWS_EXECUTION_ENV as a switch to modify env variables like https://github.com/alixaxel/chrome-aws-lambda/blob/master/source/index.js#L6.
It can be better to have more robust check routine since I've seen it's not always defined.

@Sandstedt
Copy link

I get the same error, but not related to AWS, but Linux on a Windows 10 machine using WSL 2 (Ubuntu 20.04.1 LTS) and Puppetter: 5.5.0:
ChromeHeadless stderr: /home/sandstedt/_open-source/mojs/node_modules/puppeteer/.local-chromium/linux-818858/chrome-linux/chrome: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory

Solved it by installing all missing dependencies listed here: Puppeteer Troubleshooting

@citak
Copy link

citak commented Dec 2, 2020

This may be helpful to some, to check which libs are missing just do cd /project_folder/node_modules/puppeteer/.local-chromium/linux-version/chrome-linux/ ldd chrome | grep not make sure to change the folders to your own folders, hope this helps.

@darkphoenixff4
Copy link

darkphoenixff4 commented Dec 18, 2020

I just pushed up my code to AWS Lambda, and I'm getting the exact same error. New deployment (actually never been deployed before), NodeJs12, using the shelfio layer.

Also got the same error following the instructions in the Lambda Layers option. The resulting zip file, pushed up to AWS Lambda, triggers the same error, missing libnss3.so .

@damisparks
Copy link

Here is how I fixed mine.

  • First, I ran an update.
sudo apt-get update
  • Move to the chromium directory.
cd /project_folder/node_modules/puppeteer/.local-chromium/[linux-579032]/[chrome-linux]

but replace the bracketed directories with the directories available in your node_modules folder. There you should find the local installation of chrome and from there you ran the next command.

  • Next, run this
ldd chrome | grep not

on a Linux machine to check which dependencies are missing.
Read more here Chrome headless doesn't launch on UNIX

  • Run this to install the missing libraries or packages.
sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libgconf2-4 libasound2 libatk1.0-0 libgtk-3-0
sudo apt-get install -y libgbm-dev
  • Run this again to check if the missing packages are installed
    ldd chrome | grep not this time nothing is shown on the terminal, so you should be good.
  • In your JS code, add this
const browser = await puppeteer.launch({
    headless: true,
    args: ['--no-sandbox'] });

That is it. You are good to go. Hopefully, this helps someone. 😄

@markb-trustifi
Copy link

markb-trustifi commented Jan 14, 2021

@damisparks I have the same problem of running Puppeteer in AWS Lambda env. Lambda cannot run sudo apt-get install and not even yum install. In addition, all volumes except /tmp are read-only so, the fix provided above is irrelevant. Is there any solution for this case?

@alixaxel alixaxel removed their assignment Feb 7, 2021
@alixaxel alixaxel added the invalid This doesn't seem right label Feb 7, 2021
@alixaxel
Copy link
Owner

alixaxel commented Feb 7, 2021

@markb-trustifi This issue is now a bit all over the place to be able to understand it and follow up on it properly.

I suggest you create a new issue with code to repro it, if that's not possible try passing dumpio: true to launch().

Chances are, that if there's a missing shared library, it will be logged there.
If I know which library it is, I can try to provision it with this package.

@alixaxel alixaxel closed this as completed Feb 7, 2021
@sromano88-svc
Copy link

I had this error and the fix was to set lambda memory to 512 mb.

@looeee
Copy link

looeee commented Sep 11, 2021

sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libgconf2-4 libasound2 libatk1.0-0 libgtk-3-0

This resulted in an error:

Package libgconf2-4 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'libgconf2-4' has no installation candidate

All the other packages installed correctly. This fails:

sudo apt-get install libgconf2-4 

However, I was able to install it by using apt instead of apt-get:

sudo apt install libgconf2-4 

Once I did that puppeteer works correctly.

@ca0v
Copy link

ca0v commented Nov 11, 2021

There was a long list of dependencies, so I tried this and it worked:

sudo apt install chromium-browser

I am using Ubuntu WSL on Windows 10.

@KevinVR
Copy link

KevinVR commented Dec 24, 2021

Has anybody found a solution to this for AWS Lambda? Obviously the lib can't be installed manually to the machine.

@skilbjo
Copy link

skilbjo commented May 14, 2022

Anecdotally, I am seeing this only on the node16 runtime. When I use the node14 runtime it works fine, and when I bump to node16 I get the

Failed to launch the browser process: error while loading shared libraries: libnss3.so

error

@tomoima525
Copy link

Same here. I was trying to attempt upgrading lambda version after this update aws/aws-lambda-base-images#14 but I got the error above. It was working with v14 with the workaround mentioned in #268

@tomoima525
Copy link

@skilbjo FWIW I was able to fix the issue by using the forked package by @Sparticuz in #264

@skilbjo
Copy link

skilbjo commented May 19, 2022

hey @tomoima525 , thanks for the heads up!

my workflow:
node: 16
lib: https://www.npmjs.com/package/chrome-aws-lambda
layer: arn:aws:lambda:us-east-1:764866452798:layer:chrome-aws-lambda:25 (from https://github.com/shelfio/chrome-aws-lambda-layer)

are you saying this works?
node: 16
lib: https://www.npmjs.com/package/@sparticuz/chrome-aws-lambda
layer: https://github.com/Sparticuz/chrome-aws-lambda/blob/13.5_bin/chrome_aws_lambda.zip?rgh-link-date=2022-05-16T16%3A59%3A35Z (from #264)

?

@Sparticuz
Copy link

That layer is currently out of date. You can build your own up to date layer using my fork though

@johntravolta1
Copy link

I didn't noticed that my serverless.yml was on runtime: nodejs16.x. When I changed it to nodejs14.x I was able to run withou this error on aws lambda.
Error: "/tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory"
Fix: Changed serverless.yml to
provider:
name: aws
runtime: nodejs14.x
and redeployed

@Sparticuz
Copy link

@johntravolta1 This repo's version only supports node14, not node16. You can support node16 by running the patch found in #274

@pencilcheck
Copy link

version 6.0.0 + node14 worked for me and under limit

@hkmsadek
Copy link

hkmsadek commented Apr 9, 2023

same issue. anyone solved?

@vjrngn
Copy link

vjrngn commented Apr 21, 2023

I faced the same issue on AWS Lambda using Node 16.x. I did the following:

  1. Update memory to 512mb. (Tweak this based on your context. I was able to get away with 256mb)
  2. Use Node 14.x runtime.

Hope this helps!

@benjaminwhire
Copy link

Not really - we need Node 16.x for CDK.

@Palaksialka
Copy link

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

node version - 16x
"puppeteer-core": "^10.4.0",
"chrome-aws-lambda": "^10.1.0"

Facing the issue.

@JoaoPedroCamargo
Copy link

JoaoPedroCamargo commented Jul 16, 2024

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

node version - 16x "puppeteer-core": "^10.4.0", "chrome-aws-lambda": "^10.1.0"

Facing the issue.

I'm facing the same issue here. Anyone got it working in 2024?

I'm also running it in google cloud functions, is there any better way to run this script?

@Sparticuz
Copy link

I'm facing the same issue here. Anyone got it working in 2024?

Check out my fork @Sparticuz/chromium

@kisglaci
Copy link

@Sparticuz I've used @Sparticuz/chromium I've tried using it with AWS lambda and node 16.x, also using lambda layers, but it doesn't work (it does locally with public.ecr.aws/lambda/nodejs:18 docker)
tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory

@Sparticuz
Copy link

@kisglaci

tmp/chromium: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory

That means that the AL tar isn't being extracted, which means that the environment you are using doesn't have the standard AWS environment variables

@kisglaci
Copy link

kisglaci commented Jul 18, 2024

@Sparticuz Thanks for the info, I've tried using AWS lambda layers also, the AL tar is in the zip file, do you see any reason why isn't being extracted, I'm executing it in AWS lambda, so it has to have the necessary envs I think? Added an issue also: Sparticuz/chromium#285
image

On my local machine with public.ecr.aws/lambda/nodejs:18 docker image it does work as expected

@hex406
Copy link

hex406 commented Aug 8, 2024

Anyone facing this issue within AWS Lambda, Here's how you can fix the issue :

Just check the pull request here made by @Sparticuz :
https://github.com/alixaxel/chrome-aws-lambda/pull/274/files

You have to basically edit this file in your node_modules directory :

node_modules/chrome-aws-lambda/build/index.js

Search for this line :

    if (/^AWS_Lambda_nodejs(?:10|12|14)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) {

and replace this line with your nodejs version in OR regex block like this : (example runtime 16 taken)

    if (/^AWS_Lambda_nodejs(?:10|12|14|16)[.]x$/.test(process.env.AWS_EXECUTION_ENV) === true) {

There will be 2 places of the same line you need to replace. Then just upload your package to AWS Lambda and you will be good to go.

Hope this helps !

@mnai01
Copy link

mnai01 commented Sep 23, 2024

Downgrading to lambda Runtime: Node.js 14.x and memory 512mb worked for me

@MOSTAFAEZZAT
Copy link

The issue still exist btw I'm thinking of switching to EC2 instead of Lambda

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests