在部署的 Google Cloud Function 中不存在用于运行 Playwright 的 Chromium 可执行文件

问题描述 投票:0回答:2

TL;DR - 有谁知道在安装 Node.js 依赖项后以及在 Google Cloud Function 的生产环境中运行

npx playwright install chromium
的方法吗?

是否可以在已部署的 Google Cloud Function 中运行 Playwright?我有一个已部署的函数,在本地运行良好,但在部署时,始终失败并出现以下错误:

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

有趣的是,在使用 Playwright 之前,我成功地使用 Puppeteer 成功部署了 Google Cloud Function。 Puppeteer 也使用 Chromium,并且在定位和/或安装 Chromium 可执行文件方面没有任何问题(而且我也仅将 Chromium 与 Playwright 一起使用)。

我很确定在

npx playwright install chromium
之后运行
npm install
可以解决问题,但我不确定如何在 Google Cloud Function 的环境中运行该命令。我尝试执行以下操作:

// package.json

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

但这不起作用,因为我遇到了同样的错误(并且我不确定是否执行了后脚本)。不过运行

npx playwright install chromium
可以在本地运行。

有人在部署的 Google Cloud Function 中成功运行 Playwright 吗?

更新

执行此方法适用于远程运行,但不适用于本地运行(通过模拟的 Google Cloud 功能)。这种方法的另一个问题是我们修复了特定版本的

playwright-core
chrome-aws-lambda

有没有人有更好的解决方案?

google-cloud-functions chromium playwright
2个回答
1
投票

这并不理想,因为这是在函数执行时发生的,但我通过在函数体内调用 spawnSync 来实现这一点。

import { spawnSync } from "child_process";

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

这适用于本地(通过模拟器)和生产环境。


0
投票

我使用 anaconda powershell 提示符尝试了这个解决方案,它工作得很好

设置HTTPS_PROXY=https://192.0.2.1

pip 安装剧作家

剧作家安装

© www.soinside.com 2019 - 2024. All rights reserved.