13

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

smith007
  • 221
  • 2
  • 9
  • 1
    Are you expecting to open a real ui browser in a non ui environment like a shell? Could you share us a hello world of your puppeteer code? – JRichardsz Nov 08 '22 at 14:09
  • 1
    [Why should I not upload images of code/data/errors?](https://meta.stackoverflow.com/questions/285551/why-should-i-not-upload-images-of-code-data-errors) – ggorlen Dec 24 '22 at 15:51

4 Answers4

3

I had the exact same issue and it seems to be related to this https://github.com/puppeteer/puppeteer/issues/9128

I'm using Firebase and don't have complete control over the build process when I deploy my functions but I still have access to the build logs. From the issue above, I realized I needed to handle the cache directory and the NPM version for this to work.

As far as I can tell the problem is that the build step installs the Chrome browser needed for Puppeteer in a cache directory outside of the final image that is used for the actuall function. In that context the error message makes more sence, it can't find the browser therefor it doesn't work.

I was using Node 14 in my cloud functions which used NPM 6.14.17 in the build steps. According to the issue you need to use NPM > 7 so I upgraded my function to use Node 16.

Then I added the .puppeteerrc.cjs from https://pptr.dev/guides/configuration/#examples when testing that locally it will add a .cache directory where the Chrome installation is. This has to be ignored when deploying the cloud function or the deplot will fail due to size.

In the firebase.json add:

"functions": {
  "ignore": [
    ".cache"
  ]
},

The last step is pretty specific for Firebase and I'm not sure how this applies to your build steps etc. But this solved my issue that had the exact same error messages as you had. So double check the following:

  1. NPM version in the build step, needs to beat least v.7 - Node 16 should do this.
  2. Cache directory specified in the .puppeteerrc.cjs

It also looks like your using an old Puppeteer version, I used 19.3

Kristofer Källsbo
  • 1,047
  • 2
  • 10
  • 25
  • I arrived at the same steps as you Kristofer, but once I try and call the function I get this error again (likely because .cache is ignored). Did you get past this ? Could not find Chromium (rev. 1108766). 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). – TimL Apr 13 '23 at 12:34
  • looks like adding this to package.json was the missing bit `"gcp-build": "node node_modules/puppeteer/install.js"` – TimL Apr 13 '23 at 13:18
3

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.

Mike Knapp
  • 71
  • 4
3

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

Luba
  • 46
  • 1
0

I got this same (very missleading) error in my Puppeteer project which I run in Google Cloud Function. The issue was that Function was finishing (exiting) before the async Puppeteer script was finished.

Resolved this issue by changing the "await browser.close();" to a Promise and creating the response message in promise.then().

... only to hit next problem. My script is not downloading the csv file as expected. Works locally though...