phplaravelgoogle-chromepuppeteerbrowsershot

Error: Could not find Chrome (ver. 119.0.6045.105). This can occur if either


I'm using this Laravel package: https://github.com/spatie/browsershot to take a screenshot. It's based on Puppeteer

But when I run it returns this error

Error Output:

Error: Could not find Chrome (ver. 119.0.6045.105). This can occur if either
 1. you did not perform an installation before running the script (e.g. `npx puppeteer browsers install chrome`) or
 2. your cache path is incorrectly configured (which is: /home/sail/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
    at ChromeLauncher.resolveExecutablePath (/var/www/html/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:278:27)
    at ChromeLauncher.executablePath (/var/www/html/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:209:25)
    at ChromeLauncher.computeLaunchArguments (/var/www/html/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:103:37)
    at async ChromeLauncher.launch (/var/www/html/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:69:28)
    at async callChrome (/var/www/html/vendor/spatie/browsershot/bin/browser.cjs:91:23)
 {"exception":"[object] (Symfony\\Component\\Process\\Exception\\ProcessFailedException(code: 0): The command \"PATH=$PATH:/usr/local/bin:/opt/homebrew/bin NODE_PATH=`npm root -g` node '/var/www/html/vendor/spatie/browsershot/src/../bin/browser.cjs' '{\"url\":\"file:\\/\\/\\/tmp\\/2013666216-0476504001704712330\\/index.html\",\"action\":\"screenshot\",\"options\":{\"type\":\"jpeg\",\"quality\":100,\"args\":[\"--no-sandbox\"],\"viewport\":{\"width\":1200,\"height\":630},\"displayHeaderFooter\":false,\"newHeadless\":true}}'\" failed.

I checked the /home/sail/.cache/puppeteer/chrome and see there is an folder name linux-115.0.5790.170/chrome-linux64 so I think Chrome already downloaded success.

Do you know why?


Solution

  • OK, after 12 hours of debugging and feeling crazy, I finally made it work

    Since Puppeteer v19, the Chrome will be saved to ~/.cache/puppeteer instead of inside node_modules

    But some servers don't allow to read the ~/.cache/puppeteer, that's why it's there but still shows the Could not find error. You need to move the .cache folder to your root project.

    In my case, I create a .puppeteerrc.cjs file

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

    Make sure you uninstall and re-install the Puppeteer again:

    npm remove puppeteer
    npm install puppeteer
    

    After that, we need to install some required dependencies here https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#chrome-doesnt-launch-on-linux

    I'm using Ubuntu so I need to install:

    ca-certificates
    fonts-liberation
    libasound2
    libatk-bridge2.0-0
    libatk1.0-0
    libc6
    libcairo2
    libcups2
    libdbus-1-3
    libexpat1
    libfontconfig1
    libgbm1
    libgcc1
    libglib2.0-0
    libgtk-3-0
    libnspr4
    libnss3
    libpango-1.0-0
    libpangocairo-1.0-0
    libstdc++6
    libx11-6
    libx11-xcb1
    libxcb1
    libxcomposite1
    libxcursor1
    libxdamage1
    libxext6
    libxfixes3
    libxi6
    libxrandr2
    libxrender1
    libxss1
    libxtst6
    lsb-release
    wget
    xdg-utils