【问题标题】:firebase function Puppeteer Could not find Chromium GCPfirebase 函数 Puppeteer 找不到 Chromium GCP
【发布时间】:2022-12-19 18:13:58
【问题描述】:

我在谷歌云上使用 GCP 已经很长时间了,我想运行一个使用 Puppeteer 的云函数,但不幸的是,我收到以下错误。


未处理的错误错误:找不到 Chromium(修订版 1069273)。这可能发生,如果

  1. 您在运行脚本之前没有安装(例如npm install)或
  2. 您的缓存路径配置不正确(即:/root/.cache/puppeteer)。 对于 (2),请查看我们在 https://pptr.dev/guides/configuration 上配置 Puppeteer 的指南。 在 ChromeLauncher.resolveExecutablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ProductLauncher.js:120:27) 在 ChromeLauncher.executablePath (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:166:25) 在 ChromeLauncher.launch (/workspace/node_modules/puppeteer-core/lib/cjs/puppeteer/node/ChromeLauncher.js:70:37) 在异步 /workspace/lib/index.js:122:21 在异步 /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 };
      });
    
    

    我从here复制了一个如何在 GCP 功能中使用 Puppeteer 的示例(在我的机器上运行), 我还尝试了其他不使用 Puppeteer 的功能,它们工作正常(所以我确定问题出在 Puppeteer 上)。 我还尝试添加标志“--disable-setuid-sandbox”,但这没有用。 我正在用 Typescript 编写 firebase 函数。 我的 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"
      ]
    }
    

    根据here,我在 lib 目录中也有 .puppeteerrc.cjs 文件。

    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、firebase.json 旁边,但这并没有改变它。 我尝试删除并重新安装 node_modules。 我试着关注 StackOverflow 的问题。 Deploying firebase function with Puppeteer says chrome can't be found even thoough I have enabled --no-sandbox

    Not able to migrate firebase functions to node 10 runtime

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

【问题讨论】:

    标签: node.js firebase google-cloud-functions puppeteer


    【解决方案1】:

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

    干杯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多