How to use Puppeteer in Google Cloud Functions v2
Here's how to get Puppeteer up and running in Cloud Functions v2
Justus
owntag Founder
published November 23, 2022
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.
Become a Server Side Tagging Pro with owntag
Take control of your digital data collection with Server Side Tagging and Server Side GTM – easily hosted with owntag.