Search code examples
google-cloud-functionschromiumplaywright

Chromium executable doesn't exist for running Playwright within a deployed Google Cloud Function


TL;DR - Does anyone know of a way to run npx playwright install chromium after installing Node.js dependencies, and in the Google Cloud Function's production environment?

Is it possible to run Playwright within a deployed Google Cloud Function? I have a deployed function that works fine locally, but when deployed, consistently fails with this error:

browserType.launch: Executable doesn't exist at /root/.cache/ms-playwright/chromium-1015/chrome-linux/chrome

Fun fact, before using Playwright, I successfully used Puppeteer to successfully deploy the Google Cloud Function. Puppeteer also uses Chromium and had no problems locating and/or installing the Chromium executable (and I am also using only Chromium with Playwright).

I'm pretty sure that running npx playwright install chromium after npm install would fix the problem, but I'm not sure how to run that command in the Google Cloud Function's environment. I tried doing the following:

// package.json

...
"scripts": {
  ...
  "postinstall": "npx playwright install chromium",
  "postci": "npx playwright install chromium"
}
...

But that didn't work since I'm getting the same error (and I'm not sure if either post script was executed). Running npx playwright install chromium works locally though.

Has anyone successfully ran Playwright in a deployed Google Cloud Function?

Update

Doing this approach works when running remotely, but not locally (via an emulated Google Cloud Function). Another problem with that approach is that we're fixed on specific versions of playwright-core and chrome-aws-lambda.

Does anyone have a better solution?


Solution

  • This isn't ideal since this is happening at function execution time, but I got this working by calling spawnSync in the function's body.

    import { spawnSync } from "child_process";
    
    ...
    // in the function's body
    spawnSync("npx", ["playwright", "install", "chromium"]);
    

    This works locally (via emulators) and in production.