firebase function Puppeteer Could not find Chromium GCP



我已经使用GCP很长一段时间与谷歌云,我想运行一个云功能,使用Puppeteer,但不幸的是,我得到以下错误。


未处理的错误错误:无法找到Chromium (rev. 1069273)。如果

  1. 在运行脚本(例如,npm install)之前没有安装或
  2. 你的缓存路径配置不正确(即:/root/.cache/puppeteer)。对于(2),查看我们在https://pptr.dev/guides/configuration上配置Puppeteer的指南。在ChromeLauncher。resolveExecutablePath (/workspace/node_modules/puppeteer-core/lib/cj/操纵/节点/ProductLauncher.js: 120:27)在ChromeLauncher。executablePath (/workspace/node_modules/puppeteer-core/lib/cj/操纵/节点/ChromeLauncher.js: 166:25)在ChromeLauncher。发射(/workspace/node_modules puppeteer-core/lib/cj/操纵/节点/ChromeLauncher.js: 70:37)在async/workspace/lib/index.js:122:21在async/workspace/node_modules/firebase-functions/lib/common/providers/https.js:407:26

我的代码是


export const test = functions
.runWith({
timeoutSeconds: 120,
memory: "512MB" || "2GB",
})
.https.onCall(async (data, context) => {
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto("https://www.google.com/");
browser.close();
return { msg: "all good", status: 200 };
});

我从这里复制了一个如何在GCP函数中使用Puppeteer的示例(在我的机器上工作),我还尝试了其他不使用Puppeteer的功能,这些功能工作得很好(所以我确信问题是与Puppeteer有关)。我还尝试添加"—disabled -setuid-sandbox"标志。但这行不通。我正在用Typescript编写firebase函数。我的包。Json有以下设置:

"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^10.2.0",
"firebase-functions": "^3.21.0",
"puppeteer": "^19.4.0"
},
"devDependencies": {
"@types/puppeteer": "^7.0.4",
"typescript": "^4.6.4"
},
"private": true

我tsconfig。Json文件有如下设置:

{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ES2020",
"esModuleInterop": true,
"rootDir": "src",
},
"compileOnSave": true,
"include": [
"src",
"node_modules/@types/puppeteer/index.d.ts"
]
}

lib目录中还有. puppeteerc。

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

我试着把它添加到index.js, index.ts,package旁边。json,重火力点。Json,但这并没有改变它。我尝试删除和重新安装node_modules。我试着关注StackOverflow的问题。使用Puppeteer部署firebase功能时,即使我启用了—no-sandbox

,也无法找到chrome无法将firebase函数迁移到节点10运行时

puppeteer-in-firebase-functions-failed-to-launch-chrome

降级puppeteer帮助:

npm remove puppeteer
npm install puppeteer@16.2.0 --save-dev

我也遇到过类似的问题。我能找到的最简单的修复方法是降级到Puppeteer 16.2.0版本。这是Puppeteer的弃用版本,但它应该可以工作。

欢呼

我在使用puppeteer ^19.8.0的云功能时遇到了这个问题。问题是部署云功能时使用的构建层已经包含了chromium二进制文件。一个简单的node_modules/puppeteer/install.js说它已经在构建日志中找到了。云功能在运行时使用不同的基本容器映像。诀窍是在应用程序源目录中设置缓存目录。使用puppeteer.config.js文件在根和样例从docs:

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

GCP cloud-functions将源代码目录复制到运行时容器中,允许它找到缓存。欢呼。

最新更新