skip to Main Content

I have been using GCP for a long time with google cloud, and I wanted to run a cloud function that uses Puppeteer, but unfortunately, I am getting the following error.


Unhandled error Error: Could not find Chromium (rev. 1069273). This can occur if either

  1. you did not install before running the script (e.g., npm install) or
  2. your cache path is incorrectly configured (which is: /root/.cache/puppeteer).
    For (2), check out our guide on configuring Puppeteer at https://pptr.dev/guides/configuration.
    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

My code is


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 };
  });

I copy from here an example of how to use Puppeteer in the GCP function (worked on my machine),
I also tried other functions that don’t use Puppeteer, which work fine (so I am sure the problem is with Puppeteer).
I also tried to add the flag "–disable-setuid-sandbox" but that didn’t work.
I am writing the firebase function with Typescript.
My package.json has the following settings.

"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

My tsconfig.json file has the following setting.

{
  "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"
  ]
}

I also have in the lib directory the .puppeteerrc.cjs file according to here.

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

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

I tried adding it next to index.js, index.ts,package.json, firebase.json, but that did not change it.
I tried deleting and reinstalling node_modules.
I tried to follow StackOverflow questions.
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

3

Answers


  1. I faced a similar issue. The simplest fix that I could find was to downgrade to Puppeteer 16.2.0 version. It is a deprecated version of Puppeteer but it should work.

    Cheers

    Login or Signup to reply.
  2. downgrading puppeteer helped:

    npm remove puppeteer
    npm install [email protected] --save-dev
    
    Login or Signup to reply.
  3. I had this issue with puppeteer ^19.8.0 on cloud-functions. The issue was the build layer used when deploying cloud functions contains the chromium binaries already. A simple node_modules/puppeteer/install.js said it already found in the build log. cloud-functions use a different base container image for runtime. The trick was the set the cache directory inside the application source directory. Using puppeteer.config.js file in the root and the sample from the 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 copies the source code directory to the runtime container allowing it to find the cache. Cheers.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search