User Agents

Web browsers and everyday web tech, explained

Load Browser Extensions in Headless Puppeteer

· 4 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

Running browser extensions in a headless Puppeteer session allows automation scripts to simulate real user behavior more accurately, such as ad blocking, cookie management, or injecting custom functionality. While Puppeteer runs Chrome in headless mode by default, loading extensions requires launching Chrome in headless: false or headless: 'new' mode with specific flags, as true headless mode (legacy) does not support unpacked extensions. This guide walks through the correct setup, common pitfalls, and troubleshooting steps to ensure extensions load reliably.

What You'll Need

  • Node.js installed (version 14 or higher)
  • Puppeteer or Puppeteer Core installed via npm
  • An unpacked Chrome extension (CRX not supported directly)
  • Basic understanding of JavaScript and command-line tools
  • Chrome or Chromium browser (Puppeteer usually bundles one)

Step-by-Step

  1. Prepare the extension: Ensure your extension is unpacked. If you have a .crx file, extract it using a ZIP tool. The extension folder must contain a valid manifest.json file. Note the absolute path to this folder.
  2. Install Puppeteer: Run npm install puppeteer in your project directory. Use puppeteer-core if you want to manage Chrome/Chromium installation separately.
  3. Create a Puppeteer script: Make a new JavaScript file (e.g., extension.js). Import Puppeteer at the top with const puppeteer = require('puppeteer');.
  4. Launch Chrome with extension support: In headless mode, extensions only work in headless: 'new' or headless: false. Use the following launch options:
    const browser = await puppeteer.launch({
      headless: 'new', // or false for visible mode
      args: [
        '--disable-extensions-except=/path/to/extension',
        '--load-extension=/path/to/extension'
      ]
    });
    
    Replace /path/to/extension with the actual absolute path to your extension folder. On Windows, use backslashes or forward slashes consistently; on macOS and Linux, forward slashes are standard.
  5. Handle platform-specific paths: Use Node.js path module to avoid path issues:
    const path = require('path');
    const extensionPath = path.resolve(__dirname, 'my-extension-folder');
    
    Then insert extensionPath into the --load-extension and --disable-extensions-except arguments.
  6. Open a page and verify the extension: After launching the browser, open a new page and navigate to chrome://extensions to confirm your extension is loaded and active. Note: this URL only works in non-legacy headless modes.
    const page = await browser.newPage();
    await page.goto('chrome://extensions');
    await page.screenshot({ path: 'extensions.png' });
    
    This screenshot helps verify the extension appears and is enabled.
  7. Interact with the extension (if needed): Some extensions expose background scripts or content scripts. To interact, use page.evaluate() to run code in the page context, or target specific extension pages via their URLs (e.g., chrome-extension://[ext-id]/popup.html). You may need the extension’s ID, found in chrome://extensions when “Developer mode” is enabled.
  8. Close the browser: Always close the browser at the end to free resources:
    await browser.close();
    

Common Mistakes

  • Using headless: true (legacy mode): This disables extension loading. Always use headless: 'new' or false.
  • Relative paths in args: Chrome launcher requires absolute paths. Use path.resolve() to avoid errors.
  • Forgetting --disable-extensions-except: Without this flag, Chrome may block the loaded extension for security.
  • Loading packed CRX files: Puppeteer only supports unpacked extensions. Extract the .crx first.
  • Not waiting for extension readiness: Some extensions take time to initialize. Add delays or checks if automation depends on extension behavior.

Tips

  • Use headless: 'new' for best compatibility: It supports most desktop Chrome features while remaining invisible.
  • Log extension IDs: Note the extension’s ID from chrome://extensions to reference its internal pages reliably.
  • Test in visible mode first: Run with headless: false to debug extension UI or behavior before switching back.
  • Bundle extensions with your project: Keep the extension folder in your project directory and use relative paths resolved at runtime.
  • Use environment variables for paths: Makes scripts portable across machines.

Troubleshooting

If the extension does not load, first check the browser launch arguments. A typo in the path or missing --disable-extensions-except will silently fail. Verify the path exists and is absolute. Run the script with headless: false to visually confirm the extension appears in chrome://extensions.

If Chrome fails to start, ensure no conflicting instances are running. Use puppeteer.launch({ ignoreDefaultArgs: true }) only if necessary, but re-add critical args like --no-sandbox or --disable-setuid-sandbox in Docker environments.

If the extension loads but doesn’t function, check its manifest version. Manifest V3 is supported in recent Puppeteer versions; V2 should work but may have limitations. Also, verify required permissions and content script matches in the manifest.

Screenshots of chrome://extensions are essential for debugging. If the page doesn’t load, it may indicate an invalid headless mode. Switch to headless: false temporarily.

Frequently Asked Questions

Can I use extensions in true headless mode?

No. Legacy headless: true does not support extensions. Use headless: 'new' instead.

How do I find my extension’s ID?

Load chrome://extensions in the Puppeteer browser with Developer mode enabled. The ID appears under each extension.

Can I load multiple extensions?

Yes. List multiple paths in --load-extension separated by commas: --load-extension=/path1,/path2.

Do I need Chrome installed separately?

No. Puppeteer bundles Chromium. But ensure your extension is compatible with the bundled version.

Why does chrome://extensions show nothing?

This usually means the launch args are incorrect or headless mode is incompatible. Verify arguments and use headless: 'new'.

Can I interact with extension popups or options pages?

Yes. Navigate to chrome-extension://[ext-id]/popup.html or similar URLs using page.goto(), then interact via Puppeteer methods.