How to Close a Puppeteer Browser After Using
In this article
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
- 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(). - Perform your automation tasks.
Open pages, click elements, scrape data, or take screenshots. Keep the browser reference in scope. - 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. - Use
try/finallyto guarantee closure.
Wrap your automation code in atryblock and putawait browser.close()in thefinallyblock. This ensures the browser closes even if an error occurs. - Handle the promise correctly.
If you're not usingawait, attach a.then()and.catch()to close the browser in both cases. Example:browser.close().catch(err => console.error(err)). - Exit the Node process if needed.
Afterbrowser.close(), you can callprocess.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 needbrowser.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. Usetry/finallyto avoid this. - Using
process.exit()without closing. This kills the Node process but may leave Chrome orphaned. Always close the browser first.
Tips
- Use
finallyblock 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 acatchblock. - 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)afterbrowser.close(). Also ensure you're not waiting for any unresolved promises. - Error: "Cannot read property 'close' of undefined" Means
browseris not defined. Make sure you assigned the result ofpuppeteer.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 runpkill chromeas 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).