Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions Puppeteer cannot open browser

My setup in GCF:

  1. install npm install --save puppeteer from project cloud shell

  2. edit package.json like so:

    { "dependencies": { "puppeteer": "^19.2.2" } }

  3. paste code from medium.com into index.js: https://gist.githubusercontent.com/Alezco/b9b7ce4ec7ee7f208818e395225fcbbe/raw/8554acc8b311a10e272f5d1b98dce3400945bb00/index.js

  4. deploy with 2 GB RAM, 0-3 instances, max 500s timeout

I get these errors after building or opening the URL:

  • Internal Server Error
  • Could not find Chromium (rev. 1056772). This can occur if either 1. you did not perform an installation before running the script (e.g. npm install) or 2. your cache path is incorrectly configured (which is: /workspace/.cache/puppeteer). For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.

When I run npm list both webdriver and puppeteer are installed. I suspect there is an issue this Path but I cannot figure out where it should lead. I could then provide puppeteer.launch() with argument executablePath which might solve the problem. I tried reinstalling puppeteer and changing configuration. No luck.

enter image description here

like image 952
smith007 Avatar asked Dec 19 '25 02:12

smith007


2 Answers

The following example runs on Cloud Functions Gen 2 with Node.js 16 (I did not manage to get Node.js 18 to work).

JS file with puppeteer function:

const puppeteer = require('puppeteer')


let browserPromise = puppeteer.launch(
    {
    args: [
        '--no-sandbox'
    ]
}
);

exports.productads = async (req, res) => {
  /* Your function goes here*/
}

You need to have .puppeteerrc.cjs:

const {join} = require('path');
module.exports = {
  cacheDirectory: join(__dirname, '.cache', 'puppeteer')
};

And package.json similar to this:

{
  "name": "puppeteer",
  "version": "1.0.0"
  "description": "",
  "main": "index.js",
  "scripts": {
    "gcp-build": "node node_modules/puppeteer/install.js"
  },
  "devDependencies": {
    "@google-cloud/functions-framework": "^3.1.2"
  },
  "dependencies": {
    "puppeteer": "^19.2.2"
  }
}

Both files should placed be next to the .js file: See the image

like image 137
Luba Avatar answered Dec 20 '25 18:12

Luba


In addition to adding a .puppeteerrc.cjs per Kristofer's answer, I added a postinstall script in my package.json:

"scripts": {
    ...
    "postinstall": "node node_modules/puppeteer/install.js"
  },

This fixed the problem and I was able to deploy my Google Cloud Function. This is a temporary fix until issue #9128 is fixed.

like image 25
Mike Knapp Avatar answered Dec 20 '25 18:12

Mike Knapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!