User Agents

Web browsers and everyday web tech, explained

Fix Puppeteer Navigation Failed Browser Disconnected

· 5 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

This guide explains how to resolve the "Navigation failed because browser has disconnected" error in Puppeteer. This error typically occurs when the Chrome/Chromium browser process crashes, is killed, or loses its connection to the Node.js process. You'll learn the most common causes and step-by-step fixes to get your automation running reliably again.

What You'll Need

  • A Node.js project with Puppeteer installed (npm install puppeteer)
  • Basic familiarity with Puppeteer's API (launch, newPage, goto)
  • Access to the terminal/command prompt
  • Your existing Puppeteer script that's throwing the error

Step-by-Step

  1. Identify where the error occurs. The error usually appears during page.goto(), page.waitForNavigation(), or any navigation call. Note the exact line and the surrounding code—this helps narrow down whether the browser crashed or was killed.
  2. Check for unhandled promise rejections. Wrap your navigation code in a try/catch block and log the full error stack. This can reveal if the browser process exited due to an uncaught exception in your script (e.g., calling browser.close() prematurely).
  3. Increase the default timeout. Sometimes the browser disconnects because navigation takes longer than Puppeteer's default 30-second timeout. Use page.goto(url, { timeout: 60000 }) or set a global timeout via page.setDefaultNavigationTimeout(60000).
  4. Add a 'disconnected' event listener. Attach browser.on('disconnected', () => console.log('Browser disconnected')) to see when the browser drops. This helps you correlate the disconnect with other events (e.g., memory spikes, system sleep).
  5. Check system resources. The browser may crash due to low memory or CPU. Monitor your system's resource usage while the script runs. If memory is exhausted, consider closing unused pages/tabs or using --max-old-space-size for Node.js.
  6. Run with a non-headless browser temporarily. Launch with headless: false to see if the browser window appears and crashes visibly. This can reveal errors like missing display (on Linux) or GPU issues.
  7. Disable GPU and sandbox (if on Linux/CI). Add these launch arguments to avoid common crashes:
    puppeteer.launch({
      args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu']
    })
    This is especially common in Docker or CI environments.
  8. Ensure Puppeteer and Chrome versions match. Run npm list puppeteer and npx puppeteer --version to check. If they're mismatched, update Puppeteer or reinstall with npm install puppeteer to download a compatible Chrome.
  9. Add automatic retry logic. Since disconnects can be transient, wrap your navigation in a retry function that relaunches the browser if the error occurs. For example:
    async function navigateWithRetry(page, url, retries = 3) {
      for (let i = 0; i < retries; i++) {
        try {
          await page.goto(url, { waitUntil: 'networkidle0' });
          return;
        } catch (err) {
          if (err.message.includes('browser has disconnected')) {
            console.log('Retrying...');
            // Relaunch browser and page, then continue
          } else throw err;
        }
      }
    }
    This is a pragmatic solution for flaky environments.
  10. Keep a single browser instance alive. If you're creating multiple browser instances in a loop, reuse one browser and open new pages instead. This reduces the chance of resource exhaustion that can cause disconnects.

Common Mistakes

  • Calling browser.close() too early. If you close the browser before navigation completes, you'll get this error. Ensure all awaits are inside try/finally blocks.
  • Ignoring the 'disconnected' event. Not listening for it means you miss the exact moment of failure.
  • Using a very old Puppeteer version. Older versions may have bugs that were fixed later. Always update to the latest stable release.
  • Running in a restricted environment without --no-sandbox. On many Linux systems, Chrome requires sandboxing unless disabled. Not doing so can cause immediate crashes.

Tips

  • Use a dedicated user-data-dir. Launch with userDataDir: './tmp-profile' to avoid conflicts with existing Chrome profiles.
  • Log browser process ID. You can get it via browser.process().pid and check if it's still alive in your OS's process list.
  • Set a reasonable waitUntil. Using 'networkidle0' can be more stable than 'load' for pages with heavy scripts, but it may take longer.
  • Consider using puppeteer-core. If you have a system Chrome, use puppeteer-core and specify executablePath to avoid downloading a separate browser.

Troubleshooting

If the error persists after following the steps:

  • Check Chrome's crash dumps. Look in the temporary directory (e.g., /tmp/puppeteer_dev_chrome_profile on Linux) for crash reports. They can indicate why the browser died.
  • Run a minimal test. Create a simple script that only launches the browser and navigates to a static page like about:blank. If that works, the issue is likely with the specific site you're visiting.
  • Test with a different site. If the error only happens on certain sites, it could be due to heavy JavaScript, pop-ups, or redirects. Try adding page.setJavaScriptEnabled(false) temporarily to see if that helps.
  • Check for zombie processes. After a crash, Chrome may leave orphan processes. Kill them with pkill -f chrome (Linux/macOS) or taskkill /F /IM chrome.exe (Windows) before retrying.
  • Verify your Node.js version. Puppeteer requires Node.js 18 or higher. Run node -v to confirm.

Frequently Asked Questions

Why does this error happen randomly even on simple pages?

Random disconnects often stem from system resource pressure (memory/CPU) or Chrome's own crash recovery. It can also happen when the system sleeps or the network drops.

Does this error mean my code is wrong?

Not necessarily. It's often an environment issue (e.g., missing dependencies, sandbox restrictions, or resource limits). The steps above help isolate the cause.

Can I prevent the browser from disconnecting by using a different browser?

Yes, you can try Firefox via Puppeteer's puppeteer-firefox package, but the error is less common in Chrome. However, most fixes are Chrome-specific.

How do I know if the browser crashed or was killed?

Listen for the 'disconnected' event and log the process exit code. A non-zero exit code (e.g., 137 for SIGKILL) indicates a kill, while a zero exit might mean a clean close.

Should I use a headless browser or not?

Headless mode is more resource-efficient and less likely to crash due to display issues. However, some sites behave differently, so test both.

Is there a way to automatically restart the browser after a disconnect?

Yes, you can wrap your entire automation in a function that catches the error, relaunches the browser, and resumes from the last known state. This is a common pattern in production scripts.