RainMan
RainMan

Reputation: 111

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:

enter image description here

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.

Upvotes: 11

Views: 10284

Answers (5)

Tinismo
Tinismo

Reputation: 39

Puppeteer from v19 and later changed how the install of chromium is cached. In order to get it to work correctly with heroku, you need to add the following to your scripts object in your package.json:

"scripts": {
  ...
  "heroku-postbuild": "mkdir ./.cache && mv /app/.cache/puppeteer ./.cache"
},

Without the above, heroku will not include the new cache directory that puppeteer uses, and your app will fail saying it cannot find chromium.

Source: https://github.com/jontewks/puppeteer-heroku-buildpack

Upvotes: 0

I fixed that issue by adding another buildpack https://github.com/heroku/heroku-buildpack-google-chrome and two environment variables to setup puppeteer:

PUPPETEER_EXECUTABLE_PATH = /app/.apt/usr/bin/google-chrome
PUPPETEER_SKIP_CHROMIUM_DOWNLOAD = true

Upvotes: -1

GoForth
GoForth

Reputation: 656

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.

Upvotes: 1

Adnan Hassan
Adnan Hassan

Reputation: 115

not facing this issue with puppeteer: 17.1.3

Upvotes: -2

Cowinch
Cowinch

Reputation: 126

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/

Upvotes: 11

Related Questions