最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error while excuting chrome without headless on heroku - Stack Overflow

programmeradmin1浏览0评论

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('');//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(316)] Bad NaCl helper startup ack (0 bytes)
app[web.1]:
app[web.1]:
app[web.1]: 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 ?

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 ?

Share Improve this question asked Dec 28, 2017 at 14:11 Neji SoltaniNeji Soltani 1,5114 gold badges24 silver badges43 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 26

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.

See the troubleshooting guide for more help.

Here is what worked for me. First, I clear all my buildpacks and then I added the puppeteer-heroku-buildpack and the heroku/nodejs one:

$ heroku buildpacks:clear
$ heroku buildpacks:add --index 1 https://github.com/jontewks/puppeteer-heroku-buildpack
$ heroku buildpacks:add --index 1 heroku/nodejs
发布评论

评论列表(0)

  1. 暂无评论