User Agents

Web browsers and everyday web tech, explained

Puppeteer Core with Chrome Extensions: Setup Guide

· 6 min read · Extensions

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

This guide shows you how to load and automate a Chrome extension using puppeteer-core instead of the full Puppeteer package. This is useful when you already have Chrome installed and want to test an extension's UI, trigger its background scripts, or verify its behavior in a real browser context. You'll need Node.js (v14 or later), a Chrome or Chromium browser installed, and the extension's unpacked folder (the one containing its manifest.json).

What You'll Need

  • Node.js and npm installed
  • Google Chrome or Chromium (stable or dev channel)
  • Your extension's unpacked source folder
  • Basic familiarity with JavaScript and async/await

Step-by-Step

  1. Install puppeteer-core – Run npm install puppeteer-core in your project directory. Unlike full Puppeteer, this does not download a browser; it uses the one you point it to.
  2. Find your browser executable path – On Windows, Chrome is typically at C:\Program Files\Google\Chrome\Application\chrome.exe. On macOS, it's /Applications/Google Chrome.app/Contents/MacOS/Google Chrome. On Linux, it's often /usr/bin/google-chrome or /usr/bin/chromium. Verify the path exists.
  3. Create a launch script – Write a JavaScript file, e.g., test-extension.js, with the following base code:
    const puppeteer = require('puppeteer-core');
    
    (async () => {
      const browser = await puppeteer.launch({
        executablePath: '/path/to/chrome',
        headless: false, // extensions only work in headful mode
        args: [
          `--disable-extensions-except=${pathToExtension}`,
          `--load-extension=${pathToExtension}`
        ]
      });
      const page = await browser.newPage();
      // ... your test code ...
      await browser.close();
    })();
    Replace pathToExtension with the absolute path to your extension folder.
  4. Set the extension path correctly – The path must point to the folder containing the manifest.json. Use an absolute path, not a relative one. On Windows, use forward slashes or escaped backslashes.
  5. Launch with headless: false – Chrome extensions are not supported in headless mode (except in newer headless 'new' mode, but it's unreliable). Use headless: false to see the browser window.
  6. Access the extension's service worker or background page – After launching, get the service worker (MV3) or background page (MV2) using:
    // For Manifest V3 service worker
    const worker = await browser.serviceWorker();
    // For Manifest V2 background page
    const target = await browser.waitForTarget(t => t.type() === 'background_page');
    const bgPage = await target.page();
    If you need to interact with the extension's popup, open it via chrome-extension://<extension-id>/popup.html in a new page.
  7. Find the extension ID – The extension ID is generated from the path. You can find it by visiting chrome://extensions in a regular Chrome instance with the extension loaded, or by reading the key field in the manifest (if present). For a stable ID, you can add a key to your manifest.
  8. Write your test logic – Use page.goto() to navigate to a test website, then interact with the DOM or the extension's injected content scripts. For example, if your extension adds a button, you can click it with page.click().
  9. Handle extension popup interactions – To test the popup, open a new page with the popup URL and use page.waitForSelector() to ensure it loads. Note that the popup may close when it loses focus, so keep the page active.
  10. Close the browser – Always call await browser.close() in a finally block to avoid hanging processes.

Common Mistakes

  • Using full Puppeteer instead of puppeteer-core – Full Puppeteer downloads its own Chromium, which may not support extensions properly. Stick with puppeteer-core and your installed Chrome.
  • Wrong executable path – If the path is incorrect, Puppeteer throws an error like Failed to launch the browser process. Double-check the path.
  • Relative extension path – Relative paths often fail. Use path.resolve() to convert to absolute.
  • Forgetting --disable-extensions-except – Without this flag, Chrome may not load the extension at all, or it may load all installed extensions, which can interfere.
  • Headless mode – Extensions simply don't work in classic headless. If you must use headless, try headless: 'new' but expect potential issues.
  • Not waiting for the service worker – The service worker may not be ready immediately. Use await browser.serviceWorker() which waits for it.

Tips

  • Use a dedicated user data directory – Add --user-data-dir=/tmp/puppeteer-profile to avoid conflicts with your existing Chrome profile and to keep the test isolated.
  • Log the extension ID – After launching, you can get the service worker URL and extract the ID from it: const worker = await browser.serviceWorker(); console.log(worker.url()) – the ID is the part between chrome-extension:// and the next slash.
  • Set a generous timeout – Extension loading can be slow. Increase page.setDefaultTimeout(30000) if needed.
  • Use --no-sandbox on Linux CI – If running as root or in a container, add --no-sandbox to the args to avoid sandbox errors.
  • Test with a simple extension first – Create a minimal extension with a manifest and a content script to verify your setup before testing your real extension.

Troubleshooting

  • Error: "Failed to launch the browser process" – Check the executable path. Also ensure you have permissions to run Chrome and that no other Chrome instance is using the same user data directory.
  • Extension not loaded – Verify the extension path is correct and contains a valid manifest.json. Check Chrome's console output (run with --enable-logging) for errors.
  • Service worker is undefined – If using MV3, the service worker may be inactive. Navigate to a page where the extension runs, or call chrome.runtime.getBackgroundPage() from the extension itself. Alternatively, use browser.waitForTarget(t => t.type() === 'service_worker').
  • Popup closes immediately – The popup loses focus when you interact with other pages. Use page.bringToFront() or open the popup in a new tab instead.
  • Content scripts not injecting – Ensure the extension's content script matches the URL you're visiting. Check the manifest's content_scripts matches. Also, the page must be loaded after the extension is active; use page.goto() after the browser is launched.
  • Chrome crashes on launch – This often happens when using an incompatible user data directory. Use a fresh directory via --user-data-dir.

Frequently Asked Questions

Can I use puppeteer-core with a packed .crx file?

No, Puppeteer only loads unpacked extensions. You need to extract the .crx file (it's a zip) or use the source folder.

Does headless: 'new' work with extensions?

In some recent Chrome versions, the new headless mode supports extensions, but it's not guaranteed. Test it; if it fails, use headful.

How do I get the extension ID dynamically?

After launching, get the service worker or background page URL and parse the ID. For MV3, const worker = await browser.serviceWorker(); const id = new URL(worker.url()).host;

Can I automate the extension's options page?

Yes, open the options page URL (e.g., chrome-extension://<id>/options.html) in a new page and interact with it like any other page.

Why does my extension work in a normal browser but not in Puppeteer?

Common reasons: missing flags, wrong user data directory, or the extension requires a specific browser version. Also, ensure you're not using a stripped-down Chromium build.

Is there a way to run this on a CI server?

Yes, install Chrome on the CI machine, set the executable path accordingly, and use --no-sandbox if running as root. Headful mode requires a display; use xvfb on Linux.