Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Node.js · updated 2026-11-03

Thrown by Node's ServerResponse when code sets a header or status after the response already started. In Express apps the stack runs through express/lib/response.js, and the client usually received the first response just fine.

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (node:_http_outgoing:699:11)
    at ServerResponse.header (/app/node_modules/express/lib/response.js:794:10)
    at ServerResponse.send (/app/node_modules/express/lib/response.js:174:12)
    at ServerResponse.json (/app/node_modules/express/lib/response.js:278:15)
    at /app/src/routes/users.js:18:21 {
  code: 'ERR_HTTP_HEADERS_SENT'
}

Quick fix

Make every code path send exactly one response: write return res.status(404).json(...) instead of calling res.json() and carrying on. The frame in your own route file marks the second send. In error middleware, hand off to next(err) when res.headersSent is already true.

  1. Open the stack frame in your route file; that line is the second response.
  2. Put return in front of every res.send, res.json, res.redirect and res.render.
  3. Remove next() calls that follow a response in the same middleware.
  4. Check that callbacks and timers cannot respond after another path already did.
  5. In error handlers, check res.headersSent and call next(err) if it is true.
javascript
app.get('/users/:id', async (req, res, next) => {
  try {
    const user = await db.user.findUnique({ where: { id: req.params.id } });
    if (!user) {
      return res.status(404).json({ error: 'not found' }); // return stops here
    }
    res.json(user);
  } catch (err) {
    next(err);
  }
});

app.use((err, req, res, next) => {
  if (res.headersSent) return next(err);
  res.status(500).json({ error: 'internal error' });
});
Fix it with your coding agent

Paste this into Claude Code, Cursor or Codex in the project that throws it. The agent reads this guide in plain text and applies the fix that matches your stack.

prompttext
Read https://runheed.com/errors/err-http-headers-sent.md and fix this error in this project.

Why it happens

Missing return after an early response

if (!user) res.status(404).send('not found') without return lets execution fall through to the success path below it, which then calls res.json(user) and sends a second response.

next() after responding

Middleware that sends a response and then calls next() passes the request to a later handler that responds again. Calling next(err) from a catch block after res.json already ran has the same effect.

Sending inside a loop or a repeated callback

res.json inside forEach over results, or inside a callback that fires more than once (stream data events, a library that calls back on both success and error). The first iteration responds; every later one throws.

Timeout middleware races the real handler

A timeout middleware sends a 503 and the slow handler finishes later and tries to send its own result. Nothing cancels the slower path, so both run to completion.

Headers set after the body started

res.setHeader or res.cookie called after res.write or stream.pipe(res) already began. Headers go out with the first body chunk, so Node rejects any later header change.

Other fixes

  • With a timeout middleware, check res.headersSent (or req.timedout) before sending the late result.
  • Pick one place to handle async errors: respond in the catch or call next(err), never both.
  • Set cookies and headers before piping a stream or calling res.write.

Same error, other wording

  • Error: Can't set headers after they are sent.
  • Error [ERR_HTTP_HEADERS_SENT]: Cannot write headers after they are sent to the client
  • Error [ERR_HTTP_HEADERS_SENT]: Cannot remove headers after they are sent to the client

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and, when you set it, the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find ERR_HTTP_HEADERS_SENT errors in production, identify the route and the two code paths that respond to the same request, and add the missing return or res.headersSent check.

Setup for Node.js and Express, Fastify or Vercel Functions, then have your agent fix and resolve it.

Related errors

More in Network and HTTP

Other error topics

Free tools: Source map stack trace decoder · Uptime and SLA downtime calculator · Error budget calculator · Stack trace formatter · JSON log viewer · HTTP status code reference.

References: Node.js HTTP: response.headersSent and setHeader, Node.js errors: ERR_HTTP_HEADERS_SENT

Spotted a mistake? Email hello@greatwork.company. About Runheed.