简体   繁体   中英

Puppeteer error on Heroku: Could not find Chromium

I'm a little new to deploying/hosting Node apps and Puppeteer.

But, I'm facing an issue though with my app on Heroku when trying to use Puppeteer.

The full error is:


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

My code for index.js is:


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

}

And these are my build packs in Heroku:

在此处输入图像描述

I've been battling with these for a few days and tried everything:(

Thank you!!

I've tried adding the necessary flags:

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

And also the build pack: https://github.com/jontewks/puppeteer-heroku-buildpack

These are the common solutions people have mentioned on other issue threads.

I actually just came across this problem today. To break down why this is happening: Puppeteers new v19 updates how .cache is stored. This causes issues with builds and deployment. As such if your build can't find .cache then it can't find the chromium and it crashes.

Your two options are to tell your program to run a v18 version of puppeteer or simply put this config in the same directory as your 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'),
};

Make sure to call it .puppeteerrc.cjs and afterwards, uninstall then reinstall puppeteer and add .cache to your .gitignore

If this still doesn't work you can try other config options at:
https://pptr.dev/guides/configuration/

not facing this issue with puppeteer: 17.1.3

I ran into this problem the other day. I built/packaged an Electron app with Puppeteer on Windows Server 2022 (a Vultr instance) and then tried running the built executable on a Windows 11 Home laptop. I tried the officially suggested solution of using the .puppeteerrc.cjs file. It did not work.

The only solution that worked for me was downgrading to Puppeteer 18.1.0. Hope that helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM