Heroku上的Puppeteer错误:找不到Chromium

11

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

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

完整的错误信息如下:


Error: Could not find Chromium (rev. 1056772). This can occur if either

2022-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

2022-11-10T06:44:07.938983+00:00 app[web.1]:  2. your cache path is incorrectly configured (which is: /app/.cache/puppeteer).

2022-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.

2022-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)

2022-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)

2022-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)

2022-11-10T06:44:07.938985+00:00 app[web.1]:     at Object.<anonymous> (/app/index.js:29:145)

2022-11-10T06:44:07.938985+00:00 app[web.1]:     at Module._compile (node:internal/modules/cjs/loader:1159:14)

2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)

2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module.load (node:internal/modules/cjs/loader:1037:32)

2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Module._load (node:internal/modules/cjs/loader:878:12)

2022-11-10T06:44:07.938986+00:00 app[web.1]:     at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

2022-11-10T06:44:07.938987+00:00 app[web.1]:     at node:internal/main/run_main_module:23:47

我的index.js的代码如下:


const puppeteer = require('puppeteer-extra')

const RecaptchaPlugin = require('puppeteer-extra-plugin-recaptcha')

const StealthPlugin = require('puppeteer-extra-plugin-stealth')

const { executablePath } = require('puppeteer')

const axios = require('axios')

//pupeteer plugins

puppeteer.use(

    RecaptchaPlugin({

        provider: {

            id: '2captcha',

            token: 'XXX'

        },

        visualFeedback: true //colorize reCAPTCHAs (violet = detected, green = solved)

    })

)

puppeteer.use(StealthPlugin())

//pupeteer crawl

try {

    puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'], headless: true, executablePath: executablePath(), ignoreHTTPSErrors: true }).then(async browser => {

        console.log('Running tests..')

        const page = await browser.newPage()

        await page.goto('https://bot.sannysoft.com')

        await page.setViewport({ width: 1680, height: 857 })

        await page.waitForTimeout(5000)

        await page.screenshot({ path: 'testresult.png', fullPage: true })

        await browser.close()

        console.log(`All done, check the screenshot. ✨`)

    })

} catch (error) {

    console.error(error);

}


以下是我的Heroku构建包: enter image description here 我已经尝试了一些方法,但仍无法解决问题 :(
谢谢!
我尝试添加必要的标志: args: ['--no-sandbox', '--disable-setuid-sandbox'] 还使用了这个构建包:https://github.com/jontewks/puppeteer-heroku-buildpack 这些是其他问题线程中人们提到的常见解决方案。

如果我是你,我会使用Chromium的路径作为Puppeteer中的executablePath来部署Docker容器。使用自定义驱动程序比Puppeteer安装的驱动程序要好得多。 - Partho KR
同样的问题在这里。 - conor909
1
@ParthoKR,您能详细说明一下如何实现吗?听起来是一个有趣的选择。 - RainMan
5个回答

10

今天我刚遇到了这个问题。为了分解为什么会出现这种情况:Puppeteer的新版本v19更新了.cache的存储方式。这会导致构建和部署出现问题。因此,如果您的构建无法找到.cache,那么它就找不到Chromium并且会崩溃。

您有两个选择:要么告诉程序运行Puppeteer的v18版本,要么将以下配置放在与您的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'),
};

请确保将其命名为 .puppeteerrc.cjs ,然后卸载重新安装 puppeteer,并将 .cache 添加到您的 .gitignore 中。

如果仍然无法解决问题,您可以尝试其他配置选项:
https://pptr.dev/guides/configuration/


谢谢!这对我有用。Puppeteer v19.7.2。 - legshampoo
@legshampoo,你也在使用buildpack吗?我遇到了同样的问题。 - The Little Cousin
@TheLittleCousin 嗯,我在回复中有点过于急躁了...不知何故,它只在v19.7.2下工作了一次,然后就停止了。我目前安装的是puppeteer@18.1.0,但我主要使用puppeteer-cluster@0.23.0来启动多个实例。是的,我正在使用buildpacks heroku/nodejs和https://github.com/jontewks/puppeteer-heroku-buildpack。我在部署中使用cluster,并且它正在工作。 - legshampoo

1

前几天我遇到了这个问题。我在Windows Server 2022(一个Vultr实例)上使用Puppeteer构建/打包了Electron应用程序,然后尝试在Windows 11 Home笔记本电脑上运行构建的可执行文件。我尝试了官方建议的解决方案,即使用.puppeteerrc.cjs文件,但它没有起作用。

唯一对我有用的解决方案是降级到Puppeteer 18.1.0。希望能帮助到某些人。


0
从v19版本开始,Puppeteer更改了对Chromium的安装缓存方式。为了使其能够在Heroku上正常工作,您需要将以下内容添加到package.json文件中的scripts对象中:
"scripts": {
  ...
  "heroku-postbuild": "mkdir ./.cache && mv /app/.cache/puppeteer ./.cache"
},

没有上述步骤,Heroku将不会包含Puppeteer使用的新缓存目录,导致您的应用程序无法找到Chromium而失败。
来源:https://github.com/jontewks/puppeteer-heroku-buildpack

-1

-2

使用puppeteer时没有遇到这个问题:17.1.3


对我来说是这样的。 - YanivGK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接