User Agents

Web browsers and everyday web tech, explained

How to Close a Puppeteer Browser After Using

· 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

Closing a Puppeteer browser properly frees system resources, prevents orphaned Chrome processes, and ensures your script exits cleanly. The core method is browser.close(), but you must call it after all operations and handle errors so the browser always closes, even if something fails.

What You'll Need

  • Node.js installed (v12 or later recommended)
  • Puppeteer installed in your project (npm install puppeteer)
  • A basic script that launches a browser (e.g., const browser = await puppeteer.launch())

Step-by-Step

  1. Launch the browser and store the reference.
    Always assign the browser instance to a variable so you can close it later. For example: const browser = await puppeteer.launch().
  2. Perform your automation tasks.
    Open pages, click elements, scrape data, or take screenshots. Keep the browser reference in scope.
  3. Call browser.close() after all tasks are done.
    Place this line at the end of your async function: await browser.close(). This closes all pages and the browser process.
  4. Use try/finally to guarantee closure.
    Wrap your automation code in a try block and put await browser.close() in the finally block. This ensures the browser closes even if an error occurs.
  5. Handle the promise correctly.
    If you're not using await, attach a .then() and .catch() to close the browser in both cases. Example: browser.close().catch(err => console.error(err)).
  6. Exit the Node process if needed.
    After browser.close(), you can call process.exit(0) to force the script to end, especially if there are lingering handles (like open sockets).

Common Mistakes

  • Forgetting to close the browser. This leaves Chrome processes running in the background, consuming memory and CPU.
  • Closing only a page, not the browser. Using page.close() closes the tab but keeps the browser running. You need browser.close() to shut down everything.
  • Calling browser.close() before async operations finish. If you close prematurely, you'll get errors like "Protocol error: Connection closed." Always await all page interactions first.
  • Not handling errors. If an exception occurs before browser.close(), the browser stays open. Use try/finally to avoid this.
  • Using process.exit() without closing. This kills the Node process but may leave Chrome orphaned. Always close the browser first.

Tips

  • Use finally block for robustness. Example: try { ... } finally { await browser.close(); }
  • Set a timeout for the browser launch. If the browser hangs, you can force-close it with await browser.close() in a catch block.
  • For quick scripts, use puppeteer.launch().then(async browser => { ... await browser.close(); }) to keep everything in one chain.
  • If you use page.close() for individual tabs, remember to close the browser at the end.
  • Check for background tasks. If you have timers or intervals, clear them before closing to avoid hanging.

Troubleshooting

  • Browser doesn't close and script hangs. This often happens if there are open handles like WebSocket connections. Try adding process.exit(0) after browser.close(). Also ensure you're not waiting for any unresolved promises.
  • Error: "Cannot read property 'close' of undefined" Means browser is not defined. Make sure you assigned the result of puppeteer.launch() to a variable.
  • Error: "Protocol error: Connection closed" This usually means the browser crashed or was closed externally. Wrap your code in try/catch and log the error for debugging.
  • Chrome processes remain after script ends. Use your OS's task manager to kill them, but also fix your script to always call browser.close(). In Linux/macOS, you can run pkill chrome as a last resort.
  • Script exits but browser stays open. If you're using headless: false, the browser window may stay open if you don't close it. Double-check that your code reaches the close call.

Frequently Asked Questions

Do I need to close each page individually?

No, browser.close() automatically closes all open pages. You only need page.close() if you want to close a specific tab while keeping the browser running.

What's the difference between browser.close() and browser.disconnect()?

close() terminates the browser process completely. disconnect() only disconnects the Node.js script from the browser, leaving the browser running (useful for remote debugging).

Can I reuse the browser after closing it?

No, once closed, the browser instance is unusable. You must launch a new one with puppeteer.launch().

Does browser.close() wait for pending operations?

It waits for the browser to close, but it does not wait for your async tasks to finish. Ensure you await all page interactions before calling close.

What if my script crashes unexpectedly?

Use a try/finally block or a global process.on('exit') handler to close the browser. But the best practice is to always close in a finally block.

Is it okay to call browser.close() multiple times?

It's safe, but unnecessary. Calling it twice might throw an error if the browser is already closed. Guard with a check like if (browser && browser.connected).