Load Browser Extensions in Headless Puppeteer
In this article
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
- 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.jsonfile. Note the absolute path to this folder. - Install Puppeteer: Run
npm install puppeteerin your project directory. Usepuppeteer-coreif you want to manage Chrome/Chromium installation separately. - Create a Puppeteer script: Make a new JavaScript file (e.g.,
extension.js). Import Puppeteer at the top withconst puppeteer = require('puppeteer');. - Launch Chrome with extension support: In headless mode, extensions only work in
headless: 'new'orheadless: false. Use the following launch options:
Replaceconst browser = await puppeteer.launch({ headless: 'new', // or false for visible mode args: [ '--disable-extensions-except=/path/to/extension', '--load-extension=/path/to/extension' ] });/path/to/extensionwith the actual absolute path to your extension folder. On Windows, use backslashes or forward slashes consistently; on macOS and Linux, forward slashes are standard. - Handle platform-specific paths: Use Node.js
pathmodule to avoid path issues:
Then insertconst path = require('path'); const extensionPath = path.resolve(__dirname, 'my-extension-folder');extensionPathinto the--load-extensionand--disable-extensions-exceptarguments. - Open a page and verify the extension: After launching the browser, open a new page and navigate to
chrome://extensionsto confirm your extension is loaded and active. Note: this URL only works in non-legacy headless modes.
This screenshot helps verify the extension appears and is enabled.const page = await browser.newPage(); await page.goto('chrome://extensions'); await page.screenshot({ path: 'extensions.png' }); - 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 inchrome://extensionswhen “Developer mode” is enabled. - 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 useheadless: 'new'orfalse. - 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://extensionsto reference its internal pages reliably. - Test in visible mode first: Run with
headless: falseto 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. Legacyheadless: true does not support extensions. Use headless: 'new' instead.
How do I find my extension’s ID?
Loadchrome://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 tochrome-extension://[ext-id]/popup.html or similar URLs using page.goto(), then interact via Puppeteer methods.