Puppeteer 无法在 heroku 上 运行

Puppeteer unable to run on heroku

我在 heroku 上部署了一个应用程序,并添加了 puppeteer 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.

您应该可以通过 --no-sandbox and --disable-setuid-sandbox flags to puppeteer.launch():

来解决这个问题
const browser = await puppeteer.launch({
  args: [
    '--no-sandbox',
    '--disable-setuid-sandbox',
  ],
});

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

以下是对我有用的方法。首先,我清除了所有构建包,然后添加了 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

然后,将以下参数添加到 puppeteer 启动函数中:

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

最后,将其部署回 Heroku:

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

太棒了,但为了最小化、可运行的示例,我想我会分享我的完整代码和工作流程,以便 运行 一个基于 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 构建包创建一个新应用程序(参见 ):

    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>