Heroku 上的 Puppeteer 错误:找不到 Chromium

Rai*_*Man 11 javascript heroku node.js buildpack puppeteer

我对部署/托管 Node 应用程序和 Puppeteer 不太熟悉。

\n

但是,当我尝试使用 Puppeteer 时,我在 Heroku 上的应用程序遇到了问题。

\n

完整的错误是:

\n
\nError: Could not find Chromium (rev. 1056772). This can occur if either\n\n2022-11-10T06:44:07.938983+00:00 app[web.1]:  1. you did not perform an installation before running the script (e.g. `npm install`) or\n\n2022-11-10T06:44:07.938983+00:00 app[web.1]:  2. your cache path is incorrectly configured (which is: /app/.cache/puppeteer).\n\n2022-11-10T06:44:07.938983+00:00 app[web.1]: For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.\n\n2022-11-10T06:44:07.938984+00:00 app[web.1]:     at ChromeLauncher.resolveExecutablePath (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:120:27)\n\n2022-11-10T06:44:07.938984+00:00 app[web.1]:     at ChromeLauncher.executablePath (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:166:25)\n\n2022-11-10T06:44:07.938985+00:00 app[web.1]:     at PuppeteerNode.executablePath (/app/node_modules/puppeteer-core/lib/cjs/puppeteer/node/PuppeteerNode.js:162:105)\n\n2022-11-10T06:44:07.938985+00:00 app[web.1]:     at Object.<anonymous> (/app/index.js:29:145)\n\n2022-11-10T06:44:07.938985+00:00 app[web.1]:     at Module._compile (node:internal/modules/cjs/loader:1159:14)\n\n2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)\n\n2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module.load (node:internal/modules/cjs/loader:1037:32)\n\n2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module._load (node:internal/modules/cjs/loader:878:12)\n\n2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)\n\n2022-11-10T06:44:07.938987+00:00 app[web.1]:     at node:internal/main/run_main_module:23:47\n\n
Run Code Online (Sandbox Code Playgroud)\n

我的 index.js 代码是:

\n
\nconst puppeteer = require(\'puppeteer-extra\')\n\nconst RecaptchaPlugin = require(\'puppeteer-extra-plugin-recaptcha\')\n\nconst StealthPlugin = require(\'puppeteer-extra-plugin-stealth\')\n\nconst { executablePath } = require(\'puppeteer\')\n\nconst axios = require(\'axios\')\n\n//pupeteer plugins\n\npuppeteer.use(\n\n    RecaptchaPlugin({\n\n        provider: {\n\n            id: \'2captcha\',\n\n            token: \'XXX\'\n\n        },\n\n        visualFeedback: true //colorize reCAPTCHAs (violet = detected, green = solved)\n\n    })\n\n)\n\npuppeteer.use(StealthPlugin())\n\n//pupeteer crawl\n\ntry {\n\n    puppeteer.launch({ args: [\'--no-sandbox\', \'--disable-setuid-sandbox\'], headless: true, executablePath: executablePath(), ignoreHTTPSErrors: true }).then(async browser => {\n\n        console.log(\'Running tests..\')\n\n        const page = await browser.newPage()\n\n        await page.goto(\'https://bot.sannysoft.com\')\n\n        await page.setViewport({ width: 1680, height: 857 })\n\n        await page.waitForTimeout(5000)\n\n        await page.screenshot({ path: \'testresult.png\', fullPage: true })\n\n        await browser.close()\n\n        console.log(`All done, check the screenshot. \xe2\x9c\xa8`)\n\n    })\n\n} catch (error) {\n\n    console.error(error);\n\n}\n\n
Run Code Online (Sandbox Code Playgroud)\n

这些是我在 Heroku 中的构建包:

\n

在此输入图像描述

\n

我已经与这些斗争了几天并尝试了一切:(

\n

谢谢你!!

\n

我尝试添加必要的标志:

\n

args: [\'--no-sandbox\', \'--disable-setuid-sandbox\']

\n

还有构建包:https://github.com/jontewks/puppeteer-heroku-buildpack

\n

这些是人们在其他问题线程中提到的常见解决方案。

\n

小智 10

其实我今天才遇到这个问题。要解释为什么会发生这种情况:Puppeteers 新 v19 更新了.cache存储方式。这会导致构建和部署出现问题。因此,如果您的构建无法找到,.cache那么它也找不到 chromium,并且会崩溃。

您的两个选择是告诉您的程序运行 v18 版本的 puppeteer,或者只是将此配置放在与您的 index.js 相同的目录中

project-directory/.puppeteerrc.cjs

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

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

确保调用它.puppeteerrc.cjs ,然后卸载然后重新安装 puppeteer 并添加.cache到您的.gitignore

如果这仍然不起作用,您可以尝试其他配置选项:
https ://pptr.dev/guides/configuration/