10

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?

4
  • I am trying to make Webkit browser work with Playwright and got the same Executable doesn't exist error. What I found during debugging is that there was a version mismatch. browserType.launch was looking for ...\ms-playwright\webkit-1683\Playwright.exe executable while the installed version was located in ...\ms-playwright\webkit-1630. Commented Aug 5, 2022 at 18:18
  • Thanks for sharing. Are you using executablePath to point to the installed version? playwright.dev/docs/api/class-browsertype#browser-type-launch Commented Aug 6, 2022 at 15:31
  • This might be a bit unrelated but I wonder why you move from puppeteer to playwright. I'm also curious about the performance between these two. I only need chromium for my use case (no other browsers). Commented Feb 16, 2024 at 6:06
  • The reason is, for my use case at the time, I found the APIs to be improved. My playwright script was downloading CSV files, and playwright's download event made that much easier than the manual polling method I was using with puppeteer. My script went from 120 lines to 60 for the same functionality. Commented Feb 16, 2024 at 16:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does it mean it will try to install playwwright on every function call?
Running npx playwright install chromium will attempt install chromium every time it is executed.
0

I tried this solution using anaconda powershell prompt and it worked perfectly

set HTTPS_PROXY=https://192.0.2.1

pip install playwright

playwright install

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.