Puppeteer Cluster Multiple Tabs: Concurrency Guide
In this article
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
- Import the required modules. In your main script (e.g.,
index.js), importClusterfrompuppeteer-clusterand optionallypuppeteerif you need to pass a custom browser instance. - 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. - Create the cluster with options. Call
Cluster.launch()with an object specifyingconcurrency,maxConcurrency(number of tabs you want open simultaneously), andpuppeteerOptions(e.g.,headless: 'new'). - Define the task function. Pass a callback to
cluster.task()that receives aparamsobject. Inside, useparams.pageto access the page (tab) created for that task. Perform your actions—navigate, scrape, click—using that page. - 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. - Await cluster.idle() and cluster.close(). After queuing everything, call
await cluster.idle()to wait for all tasks to finish, thenawait cluster.close()to close the browser and clean up. - Handle errors per task. Add a
catchinside your task function or use thecluster.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, useCONCURRENCY_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
maxConcurrencybased on your CPU cores. A good starting point isos.cpus().length(but cap it at 10–15 for typical scraping). - Set
timeoutin 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 viaparams.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()beforecluster.idle(). Also check thatmaxConcurrencyis greater than 0. - Memory usage spikes and crashes. Reduce
maxConcurrency, close unused pages, and consider usingpage.goto()withwaitUntil: '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
popupevent and closing them, or usepage.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 atimeoutoption (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.