blog.1.image
November 23, 2022

How to use Puppeteer in Google Cloud Functions v2

by Justus

The headless browser automation library puppeteer requires some extra steps to run in Google Cloud Functions v2:

First, as with any regular puppeteer installation, you should install is as usual with

npm install puppeteer -S

Your package.json file would then look something like this:

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

Deploying the function would work fine, but once you first try to run it, you’ll get errors like this in the Cloud Function / Cloud Run logs:

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.
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)

To get rid of them, add this line to the scripts section of your package.json:

"gcp-build": "node node_modules/puppeteer/install.js"

so the entire file looks like this (plus any other lines that you might have specifically for your project):

{
  "name": "puppeteer-cloud-function",
  "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"
  }
}

The gcp-build script is a custom build step and run whenever npm install has finished. It will download the required chromium binaries and place them in the correct location so that your Cloud Function can find Chromium.

Configure cache path

You’ll also need to configure the cache path by editing your [project-directory]/.puppeteerrc.cjs file. Create one, if you don’t have one already:

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

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

Thanks to Felix Eichler for the hint.

Hint: Memory must be at least 1024MB

Another caveat when running puppeteer: If you haven’t defined your Cloud Function’s memory, you should set it to at least 1 GB or Puppeteer won’t run. You can set the allocated memory as the --memory flag to your deploy command:

gcloud functions deploy datalayer-test --memory=1024M --gen2 --runtime=nodejs16  --source=. --entry-point=hello --trigger-http --allow-unauthenticated

If that fails, try fully deleting your function and then redeploy it with the above command.

Did this article help you?

Then follow me on twitter
@justusbluemer
so you won't miss new articles here and for Web Analytics and Marketing technology updates in general :)