Fix Puppeteer Navigation Failed Browser Disconnected
In this article
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
- Identify where the crash happens. Add a
page.on('error')andbrowser.on('disconnected')listener to your script to log the event. This tells you if the crash is from the page or the whole browser. - 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.
- 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. - Disable GPU and sandbox if in a container. Add
--no-sandbox,--disable-gpu, and--disable-setuid-sandboxtoargsinpuppeteer.launch(). This is required on many CI systems. - 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.
- Add a watchdog timer. Use
Promise.racewith a timeout (e.g., 30 seconds) to abort navigation that hangs and triggers a crash. - 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. - Monitor system resources. Run your script while watching memory usage (e.g.,
topor Task Manager). If memory spikes, reduce concurrency or add delays. - Update Puppeteer and Chrome. Run
npm update puppeteerto get the latest fixes. Older versions have known crash bugs.
Common Mistakes
- Ignoring the
disconnectedevent 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-sandboxon 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-sizetoo high, which can cause the OS to kill the process instead.
Tips
- Use
puppeteer.launch({ headless: 'new' })for better stability in newer versions. - Set
protocolTimeoutin 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: trueto 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
executablePathto 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.