User Agents

Web browsers and everyday web tech, explained

Fix Puppeteer Chrome Error in Firebase Functions

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

When you deploy a Cloud Function that uses Puppeteer, you often hit the Could not find Chrome error. This happens because Firebase Functions run in a stripped-down Node.js environment that lacks a system Chrome installation. The fix is to install a compatible Chromium build inside your function's dependencies and tell Puppeteer to use it. This guide walks you through the exact steps to make Puppeteer work reliably on Firebase Functions (both gen 1 and gen 2).

What You'll Need

  • A Firebase project with Cloud Functions enabled (Blaze plan, since outbound network is required).
  • Firebase CLI installed and logged in (firebase login).
  • Node.js 18 or 20 locally (matching your function runtime).
  • Basic familiarity with the command line and your functions/ folder.

Step-by-Step

  1. Navigate to your functions folder: cd functions in your terminal.
  2. Remove any existing Puppeteer installation to avoid version conflicts: npm uninstall puppeteer.
  3. Install Puppeteer with its Chromium download skipped: npm install puppeteer-core. This package does not download a browser by default, which is what you want.
  4. Install a compatible Chromium build that runs on Cloud Functions. The most reliable option is chrome-aws-lambda (even though it's named for Lambda, it works on Firebase): npm install chrome-aws-lambda.
  5. Set the Puppeteer executable path in your function code. At the top of your function file, add:
    const chromium = require('chrome-aws-lambda');
    const puppeteer = require('puppeteer-core');
    
    exports.myFunction = functions.https.onRequest(async (req, res) => {
      let browser;
      try {
        browser = await puppeteer.launch({
          args: chromium.args,
          executablePath: await chromium.executablePath,
          headless: chromium.headless,
        });
        // ... your scraping logic ...
      } finally {
        if (browser) await browser.close();
      }
    });
  6. Increase the function's memory and timeout in functions/index.js or your deployment config. For example:
    exports.myFunction = functions.runWith({ memory: '1GB', timeoutSeconds: 120 }).https.onRequest(...)
    This prevents out-of-memory crashes and gives Chrome time to boot.
  7. Deploy: firebase deploy --only functions.
  8. Test your function by hitting its URL. If it still fails, check the logs (see Troubleshooting).

Common Mistakes

  • Using the regular puppeteer package instead of puppeteer-core. The regular package tries to download Chrome during install, which often fails or gets removed in the cloud environment.
  • Not setting executablePath. Without it, Puppeteer looks for Chrome in default system paths that don't exist on Cloud Functions.
  • Using chrome-aws-lambda with an incompatible Node version. The package supports Node 12+ but always test with your runtime.
  • Forgetting to close the browser. This causes memory leaks and can crash subsequent invocations.
  • Setting memory too low (e.g., 256MB). Chrome needs at least 512MB to run comfortably.

Tips

  • Use puppeteer-core and chrome-aws-lambda versions that are known to work together. As of this writing, puppeteer-core@^21 and chrome-aws-lambda@^10 are a safe combo.
  • If you only need to take a screenshot or scrape a simple page, consider using @sparticuz/chromium as an alternative to chrome-aws-lambda. It's actively maintained and works on Firebase.
  • Set NODE_ENV=production in your function's environment variables to reduce debug overhead.
  • Add a fallback: if chromium.executablePath returns null (e.g., in local emulation), use a locally installed Chrome path.
  • Use functions.config() to store the executable path if you ever need to change it without redeploying.

Troubleshooting

If the error persists, follow these checks in order:

  1. Check the logs: Run firebase functions:log to see the exact error message. It often says spawn ... ENOENT or Could not find Chrome.
  2. Verify the executable path: Add a log line console.log(await chromium.executablePath) and deploy. If it logs null, the Chromium binary wasn't installed. Re-run npm install and redeploy.
  3. Check dependencies: Make sure chrome-aws-lambda is in your package.json dependencies, not devDependencies. Firebase only installs production dependencies.
  4. Test locally: Run your function with firebase emulators:start to see if it works locally. If it does, the issue is likely environment-specific.
  5. Try a different Chromium package: If chrome-aws-lambda fails, switch to @sparticuz/chromium and adjust the launch code:
    const chromium = require('@sparticuz/chromium');
    const puppeteer = require('puppeteer-core');
    
    browser = await puppeteer.launch({
      args: chromium.args,
      executablePath: await chromium.executablePath(),
      headless: chromium.headless,
    });
  6. Check region: Some regions might have stricter network policies. Try deploying to us-central1 (default) if you used another.

Frequently Asked Questions

Why does Puppeteer work locally but not on Firebase Functions?

Locally, Puppeteer finds your system Chrome. On Firebase Functions, there is no Chrome installed, and the default Puppeteer package can't download one during deployment.

Can I just use the regular puppeteer package with a custom executable path?

You can, but it's not recommended because the regular package downloads a large Chromium binary during install, which may be stripped or cause deployment timeouts. Using puppeteer-core avoids that.

What is the difference between chrome-aws-lambda and @sparticuz/chromium?

Both provide a pre-built Chromium binary for serverless environments. chrome-aws-lambda is older but widely used; @sparticuz/chromium is a community fork that is more actively maintained and often has newer Chromium versions.

Do I need to set the CHROME_PATH environment variable?

No, because you're setting executablePath in the Puppeteer launch options. That overrides any default path.

Will this work on Firebase gen 2 (Cloud Run) functions?

Yes, the same approach works on both gen 1 and gen 2. Gen 2 may have slightly different resource limits, but the Chromium installation method is identical.

How do I reduce the cold start time?

Use a memory setting of 1GB or higher, and consider keeping the browser instance alive across invocations if your function is called frequently. But be careful with connection limits; it's often safer to launch per request.