简体   繁体   中英

Puppeteer in firebase functions failed to launch chrome

I try to run puppeteer in a firebase function. As I understand this https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md#running-puppeteer-on-google-cloud-functions it should work adding

 "engines": {
    "node": "8"
  },

in package.json. I get positive feedback it use firebase deploy

 functions: updating Node.js 8 function helloWorld(us-central1)...

Sadly it cashes with

Error: Error: Failed to launch chrome!
[0408/080847.149912:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.


TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md

    at startPuppeteer.then.then.then.catch.error (/srv/index.js:42:15)
    at <anonymous>
    at process._tickDomainCallback (internal/process/next_tick.js:229:7)

Tested the same code on local which worked.

That' the cloud functions code which fails.

const functions = require('firebase-functions');

const puppeteer = require('puppeteer');
let browser = ""
let page = ""

const startPuppeteer = async () => {
    browser = await puppeteer.launch();
    page = await browser.newPage()
}

const usePageInPuppeteer = async (url) => {
    await page.goto(url);
    return await page.title()
}

const closePuppeteer = async () => {
    return await browser.close();
}

const runtimeOpts = {
    timeoutSeconds: 500,
    memory: '2GB'
  }

exports.helloWorld = functions
.runWith(runtimeOpts)
.https.onRequest((request, response) => {

    //response.send()
    startPuppeteer()
    .then(() => {
        return usePageInPuppeteer('https://www.google.com')
    })
    .then(returnUse => {
        console.log(returnUse)
        return response.send(returnUse)
    })
    .then(() => {
        return closePuppeteer()
    })
    .catch(error => {
        throw new Error(error)
    });

});


That the local test, which works

const puppeteer = require('puppeteer');

let browser = ""
let page = ""

const startPuppeteer = async () => {
    browser = await puppeteer.launch();
    page = await browser.newPage()
}

const usePageInPuppeteer = async (url) => {
    await page.goto(url);
    return await page.title()
}

const closePuppeteer = async () => {
    return await browser.close();
}


startPuppeteer()
.then(() => {
    return usePageInPuppeteer('https://www.google.com')
})
.then(returnUse => {
    console.log(returnUse)
    return closePuppeteer()
})
.catch(error => {
    throw new Error(error)
});

const startPuppeteer = async () => {
    browser = await puppeteer.launch({args: ['--no-sandbox', '--disable-setuid-sandbox']});
    page = await browser.newPage()
}

works great. Thanks to https://stackoverflow.com/users/2911633/igor-ilic for the hint

you need to launch the chrome in sandbox mode. Running chrome as root is not supported directly, you can pass the argument --no-sandbox to launch it as a root user. The final code will look like this

browser = await puppeteer.launch({args: ['--no-sandbox']});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM