My setup in GCF:
install npm install --save puppeteer from project cloud shell
edit package.json like so:
{ "dependencies": { "puppeteer": "^19.2.2" } }
paste code from medium.com into index.js: https://gist.githubusercontent.com/Alezco/b9b7ce4ec7ee7f208818e395225fcbbe/raw/8554acc8b311a10e272f5d1b98dce3400945bb00/index.js
deploy with 2 GB RAM, 0-3 instances, max 500s timeout
I get these errors after building or opening the URL:
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.

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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With