You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GCP Firebase Functions中Puppeteer找不到Chromium的问题求助

在GCP云函数中运行Puppeteer时找不到Chromium的问题与解决方案

错误信息

未处理错误 Error: Could not find Chromium (rev. 1069273). 可能的原因如下:

  1. 运行脚本前未执行安装操作(例如:npm install);
  2. 缓存路径配置错误(当前路径为:/root/.cache/puppeteer)。
    错误堆栈:
    at ChromeLauncher.resolveExecutablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:120:27)
    at ChromeLauncher.executablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:166:25)
    at ChromeLauncher.launch (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:70:37)
    at async /workspace/lib/index.js:122:21
    at 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 };
  });

package.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"
  ]
}

尝试过的Puppeteer配置文件(.puppeteerrc.cjs)

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

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

已知情况

  • 本地运行代码正常,非Puppeteer相关云函数运行正常
  • 已尝试添加--disable-setuid-sandbox参数、移动配置文件位置、重新安装依赖,均未解决问题

可行解决方案

1. 改用 puppeteer-core 并指定系统Chromium路径

GCP云函数环境预装了Chromium,无需手动下载:

  • 将package.json中的puppeteer替换为puppeteer-core,版本保持一致
  • 修改云函数代码,指定系统Chromium路径:
const browser = await puppeteer.launch({
  args: ["--no-sandbox", "--disable-setuid-sandbox"],
  executablePath: "/usr/bin/chromium-browser"
});

2. 调整缓存路径至可访问目录

云函数对/root/.cache无写入权限,将缓存目录设置为项目工作目录:

  • 将.puppeteerrc.cjs放在项目根目录(与package.json同级),配置改为:
const {join} = require('path');

module.exports = {
  cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
  • 在package.json中添加预安装脚本,确保Chromium下载到指定目录:
"scripts": {
  "preinstall": "PUPPETEER_CACHE_DIR=.cache/puppeteer npm install puppeteer"
}

3. 修正内存配置

代码中memory: "512MB" || "2GB"实际仅生效512MB,不足以运行Chromium,建议设置至少1GB:

.runWith({
  timeoutSeconds: 120,
  memory: "1GB"
})

4. 确保部署时Chromium被打包

若坚持使用puppeteer,需确保部署时包含Chromium二进制文件:

  • 修改firebase.json,避免排除Chromium目录:
{
  "functions": {
    "source": ".",
    "ignore": [
      "node_modules/**/.bin",
      "node_modules/**/*.map",
      "!node_modules/puppeteer/.local-chromium/**"
    ]
  }
}

内容的提问来源于stack exchange,提问作者shaked

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:35:13