Using Puppeteer 19 on Google Cloud Functions
I have a Google Cloud Function which uses Puppeteer to convert HTML documents to PDF. I recently tried to upgrade Puppeteer to version 19. It worked fine locally, but not when deployed to Google Cloud:
Error: Could not find Chromium (rev. 1069273). 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: /root/.cache/puppeteer).
For (2), check out our guide on configuring puppeteer at https://pptr.dev/guides/configuration.
The fix was relatively straightforward:
First, add a new .puppeteerrc.cjs
file at the root of the project (documented here):
const { join } = require("path");
/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
cacheDirectory: join(__dirname, ".cache", "puppeteer"),
};
Second, add .cache
to your .gitignore file
:
# Puppeteer cache
.cache
This fixed everything for my first deployment, but it broke again on subsequent deployments. This lead to Step 3: Add a new gcp-build
item to the scripts
object in your project’s package.json
(documented here). This re-installs puppeteer after every deployment:
"scripts": {
"gcp-build": "node node_modules/puppeteer/install.js",
/* etc */
}
(Thanks to Justus Blümer for writing up the last part).