2

Puppeteer isn't working in google cloud!

That chrome error is in the picture.

enter image description here

I already implemented the suggestion here: Puppeteer error on Heroku: Could not find Chromium

I also tried to downgrade my puppeteer version from 19.x to 18.x and 17.x but no luck.

I'll try using Playwright unless someone here has an idea of how to fix this...

EDIT: I've tried using puppeteer-chromium-resolver instead of puppeteer however now I am entirely unable to deploy my cloud function on node runtime 16 and 18.

EDIT 2: I've abandoned puppeteer-chromium-resolver in favor of chrome-aws-lambda and have added the following code snippet and deployed to Google cloud functions:

const bundledChromium = require('chrome-aws-lambda');
const playwright = require('playwright-core');

(async () => {
    const browser = await Promise.resolve(bundledChromium.executablePath).then(
    (executablePath) => {
      console.log("executablePath: ", executablePath);
      if (!executablePath) {
        // local execution
        return playwright.chromium.launch({});
      }
      return playwright.chromium.launch({ executablePath });
    }
  );
})()

the statement console.log("executablePath: ", executablePath); prints "/tmp/chromium"

however I get another error:

2022-12-27 15:12:00.281 HKT
function-1ol6uqbjimrh1 Function execution started
2022-12-27 15:12:00.295 HKT
function-1ol6uqbjimrh1 executablePath: /tmp/chromium
2022-12-27 15:12:00.305 HKT
function-1ol6uqbjimrh1 Function execution took 24 ms, finished with status: 'ok'
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 browserType.launch: spawn EFAULT
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 =========================== logs ===========================
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 <launching> /tmp/chromium --disable-field-trial-config --disable-background-networking --enable-features=NetworkService,NetworkServiceInProcess --disable-background-timer-throttling --disable-backgrounding-occluded-windows --disable-back-forward-cache --disable-breakpad --disable-client-side-phishing-detection --disable-component-extensions-with-background-pages --disable-component-update --no-default-browser-check --disable-default-apps --disable-dev-shm-usage --disable-extensions --disable-features=ImprovedCookieControls,LazyFrameLoading,GlobalMediaControls,DestroyProfileOnBrowserClose,MediaRouter,DialMediaRouteProvider,AcceptCHFrame,AutoExpandDetailsElement,CertificateTransparencyComponentUpdater,AvoidUnnecessaryBeforeUnloadCheckSync,Translate --allow-pre-commit-input --disable-hang-monitor --disable-ipc-flooding-protection --disable-popup-blocking --disable-prompt-on-repost --disable-renderer-backgrounding --disable-sync --force-color-profile=srgb --metrics-recording-only --no-first-run --enable-automation --password-store=basic --use-mock-keychain --no-service-autorun --export-tagged-pdf --headless --hide-scrollbars --mute-audio --blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4 --no-sandbox --user-data-dir=/tmp/playwright_chromiumdev_profile-K9OYeC --remote-debugging-pipe --no-startup-window
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 ============================================================
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 at /workspace/index.js:37:25
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 at async main (/workspace/index.js:30:21) {
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 name: 'Error'
2022-12-27 15:12:00.414 HKT
function-1ol6uqbjimrh1 }

Not sure how to proceed from here...

FINAL EDIT: I tried using AWS lambda with puppeteer in ECR and that didn't work either so I am using python selenium on pythonanywhere.com. This whole experience has been frustrating but at least selenium works.

Here's a link to a github for further details: https://github.com/Sparticuz/chromium/issues/29

Ayudh
  • 1,673
  • 1
  • 22
  • 55
  • Please no [images of code](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors). Did you search for similar posts like [1](https://stackoverflow.com/questions/74362083/cloud-functions-puppeteer-cannot-open-browser), [2](https://stackoverflow.com/questions/61930743/puppeteer-not-working-with-google-cloud-functions-in-node-10-runtime), [3](https://stackoverflow.com/questions/61986633/puppeteer-launch-in-cloud-functions-throwing-error), [4](https://stackoverflow.com/questions/74769348/firebase-function-puppeteer-could-not-find-chromium-gcp)? – ggorlen Dec 24 '22 at 15:49
  • none of those work – Ayudh Dec 25 '22 at 06:55
  • Did you try the https://www.npmjs.com/package/puppeteer-chromium-resolver? Please provide more details of all the things you tried as an [edit] to the post, with as complete information as possible (package.json, environment settings, code, other config details, all relevant logs and errors in text format, etc). Thanks. – ggorlen Dec 25 '22 at 07:14
  • Thank you for your suggestion. However, now the deployment itself is failing. – Ayudh Dec 25 '22 at 14:09
  • What error are you seeing, please? – ggorlen Dec 25 '22 at 16:50
  • `Function failed on loading user code. This is likely due to a bug in the user code.` But I know it isn't the code as I am running it locally first. – Ayudh Dec 26 '22 at 05:24
  • 1
    OK, thanks, but we're still missing the code, a package.json and a [mcve]. It's really hard to help just by guessing what your environment, version and code might be. – ggorlen Dec 26 '22 at 05:33
  • 1
    I've added a code example. – Ayudh Dec 27 '22 at 07:19

1 Answers1

4

1. Add .puppeteerrc.cjs and change default cache directory.

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

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

2. After you add .puppeteerrc.cjs, puppeteer package has to be re-installed, so you have to delete your function from GCP, and re-deploy your function.

3. Chrome will be installed within your source dir, puppeteer now can find right path for chrome.

Jun
  • 160
  • 6
  • As i am still having the same issue as the OP I gave the suggestion a try. I am using firebase Functions and installed "puppeteer": "^20.5.0", added .puppeteerrc.cjs with the recommended code. but this seems to trigger another type of error : Error: HTTP Error: 400, EntityTooLargeYour proposed upload is larger than the maximum object size specified in your Policy Document.
    Content-length exceeds upper bound on range
    PS D:\*\functions>
    – Bliv_Dev Jun 03 '23 at 08:04
  • "You have to delete your function from GCP, and re-deploy your function." Was what worked for us. – digibake Jun 14 '23 at 12:42