木偶无法在Heroku上运行

时间:2018-09-07 15:17:20

标签: node.js heroku puppeteer

我在heroku上部署了一个应用程序,并添加了伪造的Heroku buildpack。

成功重新部署后,我尝试运行它,但失败。使用heroku logs -t,我收到此错误消息:

2018-09-07T13:16:10.870497+00:00 app[web.1]: Error: Failed to launch chrome!
2018-09-07T13:16:10.870512+00:00 app[web.1]: [0907/131610.045486:FATAL:zygote_ho
st_impl_linux.cc(116)] No usable sandbox! Update your kernel or see https://chro
mium.googlesource.com/chromium/src/+/master/docs/linux_suid_sandbox_development.
md for more information on developing with the SUID sandbox. If you want to live
 dangerously and need an immediate workaround, you can try using --no-sandbox.

3 个答案:

答案 0 :(得分:3)

您应该能够通过将--no-sandbox--disable-setuid-sandbox标志传递到puppeteer.launch()来解决此问题:

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

如果这不起作用,您可能需要阅读Puppeteer官方故障排除指南:Running Puppeteer on Heroku

答案 1 :(得分:3)

这对我有用。首先,我清除所有构建包,然后添加puppeteer-heroku-buildpack和heroku / nodejs一个:

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

然后,将以下args添加到操纵up的启动函数中:

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

最后,将其部署回Heroku:

$ git add .
$ git commit -m "Fixing deployment issue"
$ git push heroku master

答案 2 :(得分:1)

This answer 很棒,但为了一个最小的、可运行的示例,我想我会分享我的完整代码和工作流程,用于启动和运行基于 Puppeteer 的网络应用程序。

请参阅 this answer 以获取简单的调度程序和时钟进程版本(尽管所有三种方法都可以在一个应用中共存,而无需执行任何特殊操作)。

package.json

{
  "name": "test-puppeteer",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "node index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1",
    "puppeteer": "^9.1.1"
  }
}

Procfile

web: node index.js

index.js

const express = require("express");
const puppeteer = require("puppeteer");

const app = express();
app.set("port", process.env.PORT || 5000);

const browserP = puppeteer.launch({
  args: ["--no-sandbox", "--disable-setuid-sandbox"]
});

app.get("/", (req, res) => {
  // FIXME move to a worker task; see https://devcenter.heroku.com/articles/node-redis-workers
  let page;
  (async () => {
    page = await (await browserP).newPage();
    await page.setContent(`<p>web running at ${Date()}</p>`);
    res.send(await page.content());
  })()
    .catch(err => res.sendStatus(500))
    .finally(async () => await page.close())
  ;
});

app.listen(app.get("port"), () => 
  console.log("app running on port", app.get("port"))
);

设置

  1. 安装 Heroku CLI 并使用 Node 和 Puppeteer 构建包创建一个新应用程序(请参阅 this answer):

    heroku create
    heroku buildpacks:add --index 1 https://github.com/jontewks/puppeteer-heroku-buildpack -a cryptic-dawn-48835
    heroku buildpacks:add --index 1 heroku/nodejs -a cryptic-dawn-48835
    

    (将 cryptic-dawn-48835 替换为您的应用名称)

  2. 部署:

    git init
    git add .
    git commit -m "initial commit"
    heroku git:remote -a cryptic-dawn-48835
    git push heroku master
    
  3. 验证它是否适用于 curl https://cryptic-dawn-48835.herokuapp.com。你应该看到类似

    <html><head></head><body><p>web running at Wed May 19 2021 02:12:48 GMT+0000 (Coordinated Universal Time)</p></body></html>