Sitemap

Run Puppeteer in Google Cloud Functions v2

1 min readApr 5, 2023

--

The latest version of Node.js in Google Cloud Function v2 is missing Chromium dependencies. Therefore, we cannot use the latest version. If you want to use latest version, try to use Google Cloud Run instead.

We should choose Node.js 16. Select the memory at least 1GB.

And create the following files:

index.js

const functions = require('@google-cloud/functions-framework');
const puppeteer = require('puppeteer');

const PUPPETEER_OPTIONS = {
headless: true,
};

functions.http('helloHttp', async (req, res) => {
const browser = await puppeteer.launch(PUPPETEER_OPTIONS);
const page = await browser.newPage();

// your logic here

res.send(`Hello ${req.query.name || req.body.name || 'World'}!`);
});

package.json

{
"scripts": {
"gcp-build": "node node_modules/puppeteer/install.js"
},
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0",
"puppeteer": "^19.8.0"
}
}

.puppeteerrc.cjs

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

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

Then it should work.

Reference

--

--

Responses (2)