Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while excuting chrome without headless on heroku

I am currently working on project where I need to build an application that needs to open an URL in a browser in order to use some functions on it.
for that I used puppeteer inside a nodejs script in order to open the browser on the server side so I can use it like an api .

Here's the code (nodejs):

app.get('/do', (req, res) => {
    console.log("ok");
    (async() => {
        var browser = await puppeteer.launch(
            { args: ['--no-sandbox','--disable-setuid-sandbox'], headless: false });
        var page = await browser.newPage();
        await page.goto('https://url.com');//i hid the url for personal reason
        await page.waitFor(1000); // to wait for 1000ms
        await page.waitFor('body div'); // to wait for the 'body div' selector in body
        await page.waitFor(() => Math.random() < 0.5); // to wait for the predicate
        await page.screenshot({
            path: 'public/photo.png'
        });

        await browser.close();
        await res.end('<html><head></title></head><body><h1><img src=photo.png ></img></h1></body></html>');
    })();

});

This code works locally but when I deploy on heroku it shows me this error :

app[web.1]: /send
app[web.1]: (node:4) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Failed to launch chrome!
app[web.1]: /app/node_modules/puppeteer/.local-chromium/linux-515411/chrome-linux/nacl_helper: error while loading shared libraries: libnss3.so: cannot open shared object file: No such file or directory
app[web.1]: [21:21:1228/131735.202176:ERROR:nacl_fork_delegate_linux.cc(316)] Bad NaCl helper startup ack (0 bytes)
app[web.1]:
app[web.1]:
app[web.1]: TROUBLESHOOTING: https://github.com/GoogleChrome/puppeteer/blob/master/docs/troubleshooting.md
app[web.1]:
app[web.1]: (node:4) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

But if I remove headless: false it works but the problem is that the url shows a warming page that I need to use a browser like chrome or mozilla or safari.

How can I resolve this issue ?

like image 897
Neji Soltani Avatar asked Dec 28 '17 14:12

Neji Soltani


1 Answers

You need to include the Puppeteer Heroku buildpack to the list of buildpacks for your app. Go to your Heroku dashboard and open your app. Go to Settings > Buildpacks > Add buildpack and use this URL.

https://github.com/jontewks/puppeteer-heroku-buildpack

When you click add buildpack, simply paste that URL into the input, and click save. On the next deploy, your app will also install the dependencies that Puppeteer needs to run.

Puppeteer on Heroku

See the troubleshooting guide for more help.

like image 119
Amit Agarwal Avatar answered Sep 28 '22 10:09

Amit Agarwal