I have been using GCP for a long time with google cloud, and I wanted to run a cloud function that uses Puppeteer, but unfortunately, I am getting the following error.
Unhandled error Error: Could not find Chromium (rev. 1069273). This can occur if either
npm install) orMy code is
export const test = functions
.runWith({
timeoutSeconds: 120,
memory: "512MB" || "2GB",
})
.https.onCall(async (data, context) => {
const browser = await puppeteer.launch({ args: ["--no-sandbox"] });
const page = await browser.newPage();
await page.goto("https://www.google.com/");
browser.close();
return { msg: "all good", status: 200 };
});
I copy from here an example of how to use Puppeteer in the GCP function (worked on my machine), I also tried other functions that don't use Puppeteer, which work fine (so I am sure the problem is with Puppeteer). I also tried to add the flag "--disable-setuid-sandbox" but that didn't work. I am writing the firebase function with Typescript. My package.json has the following settings.
"engines": {
"node": "16"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^10.2.0",
"firebase-functions": "^3.21.0",
"puppeteer": "^19.4.0"
},
"devDependencies": {
"@types/puppeteer": "^7.0.4",
"typescript": "^4.6.4"
},
"private": true
My tsconfig.json file has the following setting.
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noImplicitReturns": true,
"noUnusedLocals": true,
"outDir": "lib",
"sourceMap": true,
"strict": true,
"target": "ES2020",
"esModuleInterop": true,
"rootDir": "src",
},
"compileOnSave": true,
"include": [
"src",
"node_modules/@types/puppeteer/index.d.ts"
]
}
I also have in the lib directory the .puppeteerrc.cjs file according to here.
const {join} = require('path');
/**
* @type {import("puppeteer").Configuration}
*/
module.exports = {
// Changes the cache location for Puppeteer.
cacheDirectory: join(__dirname, '.cache', 'puppeteer'),
};
I tried adding it next to index.js, index.ts,package.json, firebase.json, but that did not change it. I tried deleting and reinstalling node_modules. I tried to follow StackOverflow questions. Deploying firebase function with Puppeteer says chrome can't be found even thoough I have enabled --no-sandbox
Not able to migrate firebase functions to node 10 runtime
puppeteer-in-firebase-functions-failed-to-launch-chrome
downgrading puppeteer helped:
npm remove puppeteer
npm install [email protected] --save-dev
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