NodeJS 傀儡无法在Heroku上运行

j1dl9f46  于 2023-03-01  发布在  Node.js
关注(0)|答案(4)|浏览(125)

我在Heroku上部署了一个应用程序,并添加了 puppet 师Heroku构建包。
成功重新部署后,我尝试运行它,但失败了。使用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.
mwg9r5ms

mwg9r5ms1#

以下是我的工作方式:首先,我清除了所有的构建包,然后添加了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
dy2hfwbg

dy2hfwbg2#

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

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

如果这不起作用,您可能需要阅读官方Puppeteer故障排除指南:在Heroku上运行 puppet 师。

7kqas0il

7kqas0il3#

This answer非常棒,但是为了使示例最小化,我想我应该分享我的完整代码和工作流,以启动和运行基于Puppeteer的Web应用程序。
请参阅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(() => 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替换为您的应用名称)
1.展开:

git init
git add .
git commit -m "initial commit"
heroku git:remote -a cryptic-dawn-48835
git push heroku master

1.验证它是否可以与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>
mwg9r5ms

mwg9r5ms4#

我已经能够解决在Heroku中安装puppeteer v19.7.2的问题,方法是将以下文件添加到我的项目的基目录中:“. puppet 剧.cjs

const {join} = require('path');

/**
 * @type {import("puppeteer").Configuration}
 */
module.exports = {
  // Changes the cache location for Puppeteer.
  cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};

Puppeteer v19移动该高速缓存目录,这个文件告诉npm安装程序将缓存目录从$HOME/.cache/puppeteer移动到current-working-directory/.cache/puppeteer。这个文件也被puppeteer在启动时读取,这样它就知道在哪里可以找到缓存文件。我已经在Heroku 22上测试过了,效果很好。记得把**.cache**添加到你的. gitignore中。

相关问题