Issues with Puppeteer in Firebase Cloud Functions on GCP: Chromium Not Found

I’m experiencing difficulties while trying to deploy a cloud function using Puppeteer on Google Cloud Platform (GCP). I’m receiving an error message indicating that Chromium cannot be located. The error detail suggests that it may be due to either failing to install dependencies beforehand (for instance, using npm install) or having an incorrect cache path configured (current path being: /root/.cache/puppeteer). Below is my code snippet:

export const myFunction = functions
  .runWith({
    timeoutSeconds: 120,
    memory: '512MB' || '2GB',
  })
  .https.onCall(async (payload, context) => {
    const browserInstance = await puppeteer.launch({ args: ['--no-sandbox'] });
    const newPage = await browserInstance.newPage();
    await newPage.goto('https://www.example.com/');

    browserInstance.close();
    return { message: 'success', code: 200 };
  });

My package.json contains:

"engines": {
    "node": "16"
  },

And the tsconfig.json settings are:

{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "ES2020",
    "esModuleInterop": true,
    "rootDir": "src"
  },
}

I’ve also placed a configuration file .puppeteerrc.cjs in the lib directory. I’ve attempted several approaches, such as reinstalling modules, reviewing related questions on StackOverflow, and tweaking the setup, but the issue persists. What steps should I take to resolve this?

You might want to ensure that the necessary Puppeteer dependencies, particularly Chromium, are included in your deployment package. A common issue with Puppeteer in environments like GCP is the environment’s restrictions and lack of essential libraries. Consider using a Puppeteer build which includes chromium binaries that work on your host OS. Alternatively, you can install necessary libraries such as fonts, ca-certificates, and libnss using apt-get in a startup function or use a Docker container with the required setup. Always test locally and verify the dependencies in a minimal setup before deploying.