User Agents

Web browsers and everyday web tech, explained

Puppeteer Cluster Multiple Tabs: Concurrency Guide

· 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

Handling multiple tabs with Puppeteer Cluster's browser concurrency lets you run several scraping or automation tasks in parallel without overwhelming your system. Instead of opening a new browser for each task, you reuse a single browser instance and manage tabs efficiently. This guide walks you through setting up and using Puppeteer Cluster with ConcurrencyType.BROWSER, which is the default and most stable option for most use cases.

What You'll Need

  • Node.js (v12 or later) installed
  • A project with npm initialized (npm init -y)
  • Puppeteer Cluster installed: npm install puppeteer-cluster
  • Puppeteer (or puppeteer-core) installed: npm install puppeteer
  • Basic familiarity with async/await in JavaScript

Step-by-Step

  1. Import the required modules. In your main script (e.g., index.js), import Cluster from puppeteer-cluster and optionally puppeteer if you need to pass a custom browser instance.
  2. Define your concurrency type. Use Cluster.CONCURRENCY_BROWSER (or the string 'browser') to reuse a single browser across all tasks. This is the default, but being explicit helps readability.
  3. Create the cluster with options. Call Cluster.launch() with an object specifying concurrency, maxConcurrency (number of tabs you want open simultaneously), and puppeteerOptions (e.g., headless: 'new').
  4. Define the task function. Pass a callback to cluster.task() that receives a params object. Inside, use params.page to access the page (tab) created for that task. Perform your actions—navigate, scrape, click—using that page.
  5. Queue your URLs or data. Use cluster.queue() to add each job. You can queue strings (interpreted as URLs) or objects with custom data. For multiple tabs, queue as many items as you want to process.
  6. Await cluster.idle() and cluster.close(). After queuing everything, call await cluster.idle() to wait for all tasks to finish, then await cluster.close() to close the browser and clean up.
  7. Handle errors per task. Add a catch inside your task function or use the cluster.on('taskerror') event to log failures without stopping the whole cluster.

Common Mistakes

  • Using CONCURRENCY_PAGE instead of BROWSER. Page concurrency opens a new tab per task but shares the browser—this is actually what you want for tabs. However, many people confuse it with browser concurrency. For multiple tabs, use CONCURRENCY_PAGE (or 'page') if you want to reuse the browser but open separate tabs. The guide title says "browser concurrency," but the practical approach for tabs is page concurrency. If you truly want multiple browser instances, use CONCURRENCY_BROWSER—but that opens separate browsers, not tabs.
  • Setting maxConcurrency too high. Each tab consumes memory. Opening 50 tabs on a 8GB machine can crash. Start with 5–10 and adjust based on your system's performance.
  • Not closing pages manually. Puppeteer Cluster handles page cleanup automatically when using page concurrency, but if you create additional pages inside a task, close them with page.close() to avoid leaks.
  • Sharing global variables between tasks. Each task runs in its own page, but your Node.js process is single-threaded. Avoid using global mutable state that tasks write to concurrently—use per-task variables or a queue.

Tips

  • Use maxConcurrency based on your CPU cores. A good starting point is os.cpus().length (but cap it at 10–15 for typical scraping).
  • Set timeout in Puppeteer options to avoid tasks hanging forever on slow sites.
  • Use cluster.on('taskerror') to log errors and continue processing other queued items.
  • If you need to pass data (like a selector) along with the URL, queue an object: cluster.queue({ url: '...', selector: '...' }) and access it via params.data.
  • For heavy pages, consider using page.setViewport() to reduce rendering load.

Troubleshooting

  • Cluster won't start or tasks fail immediately. Check that Puppeteer is installed and can launch a browser. Try running a simple script without the cluster to verify your Puppeteer setup.
  • Tasks are queued but never execute. Ensure you called cluster.queue() before cluster.idle(). Also check that maxConcurrency is greater than 0.
  • Memory usage spikes and crashes. Reduce maxConcurrency, close unused pages, and consider using page.goto() with waitUntil: 'domcontentloaded' instead of 'networkidle0' for faster navigation.
  • Errors like "Page crashed" or "Target closed". This often happens when the page navigates to a new URL or a popup opens. Handle popups by listening for the popup event and closing them, or use page.on('dialog') to handle alerts.
  • Cluster.close() hangs. If tasks are stuck, ensure you don't have infinite loops in your task function. Also, set a timeout for each task using cluster.task() with a timeout option (in ms).

Frequently Asked Questions

What's the difference between BROWSER and PAGE concurrency?

BROWSER concurrency opens a separate browser instance per task, while PAGE concurrency opens a new tab in the same browser. For handling multiple tabs, use PAGE concurrency.

Can I use Puppeteer Cluster with an existing browser?

Yes, pass puppeteerOptions: { browser: 'path/to/chrome' } or use puppeteer-core and specify the executable path.

How do I pass custom data to each task?

Queue an object instead of a string: cluster.queue({ url: '...', name: '...' }). In the task function, access it via params.data.

Is it safe to use a high maxConcurrency?

It depends on your system resources. Each tab uses about 50–100 MB of memory. Start low (5–10) and monitor your system's performance.

Can I use Puppeteer Cluster for login-based scraping?

Yes, but each task runs in a fresh tab, so you need to handle login within each task (e.g., using cookies or re-authenticating). You can also use a persistent profile by passing userDataDir in Puppeteer options.

How do I stop the cluster if something goes wrong?

Call await cluster.close() after idle(). If you need to abort mid-way, use cluster.close() directly, but be aware that pending tasks will be canceled.