Puppeteer leaves Chrome running after your Express route finishes
Node process does not exit after the Puppeteer script endsShort answer
Chrome stays alive when browser.close() is never reached. An error or early return skips it, or you called disconnect(), which leaves the process running. In Docker, orphaned Chrome processes can also linger without an init process. Close the browser in a finally block and run the container with --init.
Fix it fast
1Find out which cause you have
Count Chrome processes before and after a request. If the number goes up and does not come back down, the browser is not being closed.
ps aux | grep -c '[c]hrome'2Match what you see, then jump to its fix
- 1
Processes grow only on failed requests, such as timeouts or navigation errorsclose() is skipped because an error or early return happens firstFix - 2
The script ends cleanly but the Chrome process is still listeddisconnect() was used where close() was meantFix - 3
Many zombie chrome processes inside the container, as shown by psIn Docker, nothing reaps the leftover Chrome processesFix
- 1
What does the error look like?
Node process does not exit after the Puppeteer script endsChrome processes still listed by ps after the request has finished
Cause 1 of 3
close() is skipped because an error or early return happens first
1Check
Look for browser.close() placed at the end of the try block, or after a return. The call is asynchronous (it returns a Promise), and any throw before it means it never runs.
grep -n -B3 -A3 "browser.close" src/2Fix
Create the browser first, then put close() in a finally block, which runs after a return or a throw.
app.get('/pdf', async (req, res, next) => {
const browser = await puppeteer.launch();
try {
const page = await browser.newPage();
await page.goto(req.query.url);
res.send(await page.pdf());
} catch (err) {
next(err);
} finally {
await browser.close();
}
});3Verify
Send one failing request (a bad URL) and one good one, then count the processes again.
ps aux | grep -c '[c]hrome'Confirmed by pptr.dev
Cause 2 of 3
disconnect() was used where close() was meant
1Check
disconnect() only cuts Puppeteer's connection and leaves the browser process running. close() shuts the browser and all its pages.
grep -n "disconnect" src/2Fix
Use close() to end a browser you launched. Keep disconnect() for a browser you connected to and want to leave running.
// launched here, so close it here
await browser.close();3Verify
Run the script once and confirm no Chrome process is left.
pgrep -fl chrome || echo noneConfirmed by pptr.dev
Cause 3 of 3
In Docker, nothing reaps the leftover Chrome processes
1Check
Puppeteer's troubleshooting guide says that when a container runs your app as PID 1, Chrome can be hard to terminate properly and zombies pile up.
docker exec my-container ps aux | grep -c defunct2Fix
Start the container with an init process, either docker run --init or dumb-init in the image.
docker run --init --rm my-image3Verify
Load-test the endpoint and count defunct processes again.
docker exec my-container ps aux | grep -c defunctConfirmed by pptr.dev
How do I stop it coming back?
- Wrap every launch in try/finally, so the close call cannot be skipped.
- Reuse one browser for many requests and open and close pages, if launch time matters.
- Watch process count or memory in your monitoring, and alert when it keeps rising.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
Related fixes
Back end
Kong Gateway returns 502 'An invalid response was received from the upstream server'
upstream sent too big header while reading response header from upstream
Kong returns 502 when it cannot get a valid HTTP response from the upstream.
4 causes, each with a fixBack end
npm install fails with getaddrinfo ENOTFOUND registry.npmjs.org
npm error errno ENOTFOUND
ENOTFOUND means a DNS lookup for the host failed, so npm never connected.
3 causes, each with a fixBack end
Puppeteer times out after 30000 ms on goto, setContent or pdf
page.goto, page.setContent or page.pdf rejects after exactly 30 seconds
Thirty seconds is Puppeteer's default timeout for navigation and for page.pdf.
4 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fix