User Agents

Web browsers and everyday web tech, explained

Fix Puppeteer Navigation Failed Browser Disconnected

· 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

When Puppeteer throws Navigation failed because browser has disconnected, it means the browser process crashed or was closed unexpectedly during a page load. This usually happens due to memory pressure, a page that crashes the renderer, or the browser being killed by the OS. This guide walks you through diagnosing and fixing the issue, from quick retries to more robust configuration changes.

What You'll Need

  • A Node.js project with Puppeteer installed (npm install puppeteer)
  • Basic knowledge of async/await in JavaScript
  • Access to the terminal/command line to run your script

Step-by-Step

  1. Identify where the crash happens. Add a page.on('error') and browser.on('disconnected') listener to your script to log the event. This tells you if the crash is from the page or the whole browser.
  2. Wrap navigation in a retry loop. Simple crashes are often transient. Create a function that tries navigation up to 3 times with a delay (e.g., 1 second) between attempts.
  3. Increase the browser's memory limit. Launch Puppeteer with --js-flags="--max-old-space-size=4096" and --disable-dev-shm-usage (especially in Docker). This gives the renderer more memory and avoids shared memory issues.
  4. Disable GPU and sandbox if in a container. Add --no-sandbox, --disable-gpu, and --disable-setuid-sandbox to args in puppeteer.launch(). This is required on many CI systems.
  5. Use a fresh browser instance for critical tasks. If your script runs long, consider launching a new browser for each page load instead of reusing one. This isolates crashes.
  6. Add a watchdog timer. Use Promise.race with a timeout (e.g., 30 seconds) to abort navigation that hangs and triggers a crash.
  7. Check for memory leaks in your script. If you create many pages without closing them, the browser runs out of memory. Always await page.close() after use.
  8. Monitor system resources. Run your script while watching memory usage (e.g., top or Task Manager). If memory spikes, reduce concurrency or add delays.
  9. Update Puppeteer and Chrome. Run npm update puppeteer to get the latest fixes. Older versions have known crash bugs.

Common Mistakes

  • Ignoring the disconnected event and not logging it – you need to know if it's a browser crash vs a page crash.
  • Not handling the promise rejection – the error is thrown asynchronously; wrap your code in try/catch.
  • Using --no-sandbox on a production server without understanding the security implications – only use it in trusted environments.
  • Reusing a single browser for hundreds of pages without closing them – this exhausts memory.
  • Setting --max-old-space-size too high, which can cause the OS to kill the process instead.

Tips

  • Use puppeteer.launch({ headless: 'new' }) for better stability in newer versions.
  • Set protocolTimeout in launch options (e.g., protocolTimeout: 180000) to avoid premature disconnects on slow pages.
  • If you're scraping many pages, use a pool of browsers (e.g., 2-3) and rotate them to spread the load.
  • Consider using page.setDefaultNavigationTimeout(60000) to give more time for slow loads.
  • In Docker, always add --disable-dev-shm-usage – the default /dev/shm is too small.

Troubleshooting

If the error persists after trying the steps above:

  • Check the browser logs. Launch with dumpio: true to see Chrome's stdout/stderr. Look for lines like "out of memory" or "segfault".
  • Test with a minimal script. Create a script that just launches the browser and navigates to a simple page (e.g., example.com). If that works, the issue is in your site's content or your script's logic.
  • Try a different Chrome binary. Use executablePath to point to a system-installed Chrome/Chromium that might be more stable.
  • Reduce page complexity. Disable JavaScript with page.setJavaScriptEnabled(false) to see if a script on the page is crashing the renderer.
  • Run with --single-process. This is not recommended for production but can help isolate if the crash is from the multi-process architecture.

Frequently Asked Questions

Why does the browser disconnect only on certain pages?

Pages with heavy JavaScript, large images, or memory leaks can crash the renderer. Try disabling JavaScript or increasing the memory limit.

Is this error caused by a timeout?

No, a timeout gives a different error (e.g., "Navigation timeout"). This error specifically means the browser process died.

Can I catch this error and continue?

Yes, you can catch it in a try/catch and relaunch the browser, but you'll lose any in-memory state. Better to prevent the crash with the tips above.

Does using --no-sandbox fix it?

It fixes crashes caused by sandbox permission issues, common in Docker or CI. It's not a universal fix.

Should I use puppeteer-core instead?

Only if you have a specific Chrome installation. The regular puppeteer package manages its own Chrome, which is usually more consistent.

How do I know if it's a memory issue?

Monitor system memory during the run. If it climbs steadily and then the crash happens, it's likely memory. Add --max-old-space-size and close pages promptly.