The error
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Quick answer:
Find the route or middleware named in the first
application-owned stack frame. Check every
res.json(),
res.send(),
res.redirect(), and
res.end() path. Each request must
reach only one final response.
What ERR_HTTP_HEADERS_SENT means
An HTTP response has headers and a body. Once Node.js has sent the response headers, the application cannot start a conflicting response path and set those headers again.
In Express applications, the visible error often appears at the second response attempt. The real bug can occur earlier in the route, middleware chain, authentication callback, database callback, or error handler.
Node.js exposes
response.headersSent so code can
determine whether the response headers have
already been sent.
Common causes
- A route sends a validation error but continues executing and later sends a success response.
- Two branches of an asynchronous operation can both reach response code.
-
Middleware sends a response and then calls
next(). - A catch or error path sends a response after another path has already responded.
- A callback fires after the route has already completed.
- Authentication or authorization middleware continues after sending a rejection response.
- Multiple independent conditions send responses because the control flow is not mutually exclusive.
Fix 1: return after a final response
A common failure happens when the route sends an error response but execution continues.
Broken
app.get('/user/:id', async (req, res) => {
const user =
await getUserById(req.params.id);
if (!user) {
res.status(404).json({
error: 'User not found'
});
}
res.json({
success: true,
user
});
});
If the user is missing, the route can reach the
404 response and then continue to the second
res.json().
Repaired
app.get('/user/:id', async (req, res) => {
const user =
await getUserById(req.params.id);
if (!user) {
return res.status(404).json({
error: 'User not found'
});
}
return res.json({
success: true,
user
});
});
Fix 2: make response branches exclusive
If several conditions can run during the same request, structure them so only one response path can complete.
app.post('/orders', async (req, res) => {
if (!req.body?.email) {
return res.status(400).json({
error: 'Email is required'
});
}
const order =
await createOrder(req.body);
return res.status(201).json({
success: true,
order
});
});
The validation response ends that request path. The order creation path runs only after validation succeeds.
Fix 3: do not call next after the final response
Broken middleware
function requireUser(req, res, next) {
if (!req.user) {
res.status(401).json({
error: 'Authentication required'
});
}
next();
}
The middleware sends a 401 response but still continues into the next handler.
Repaired middleware
function requireUser(req, res, next) {
if (!req.user) {
return res.status(401).json({
error: 'Authentication required'
});
}
return next();
}
Fix 4: check asynchronous response paths
Database queries, API calls, timers, callbacks, and other asynchronous work can make duplicate response bugs harder to see.
app.get('/report', async (req, res) => {
try {
const report =
await buildReport();
return res.json({
success: true,
report
});
} catch (error) {
console.error(error);
return res.status(500).json({
success: false,
error: 'Unable to build report'
});
}
});
Trace every asynchronous branch and confirm that success and failure cannot both send responses for the same request.
Use res.headersSent as a diagnostic, not a bandage
Node.js exposes
res.headersSent. It can help confirm
that another path has already begun the response.
if (res.headersSent) {
console.error(
'Response was already started'
);
return;
}
Do not use this check to hide broken control flow everywhere. The stronger repair is to find why two code paths are trying to complete the same request.
Check centralized error middleware
An Express error handler should avoid attempting a brand-new response when headers have already been sent.
function errorHandler(
error,
req,
res,
next
) {
console.error(error);
if (res.headersSent) {
return next(error);
}
return res.status(500).json({
success: false,
error: 'Internal server error'
});
}
app.use(errorHandler);
ERR_HTTP_HEADERS_SENT repair checklist
- Open the first application-owned file in the stack trace.
- Find every response call in that route and its middleware.
- Identify which response runs first.
- Return immediately after each final response path.
-
Do not call
next()after sending a terminal response. - Check asynchronous callbacks and promise paths for late responses.
- Check catch blocks and centralized error middleware for duplicate response attempts.
- Retest success, validation failure, authentication failure, and exception paths separately.
Still getting ERR_HTTP_HEADERS_SENT?
Paste the complete error and stack trace into 0-69. The Express / Node.js repair engine can classify the duplicate-response failure and prepare specific repair steps and copy-ready code for the detected pattern.
Analyze My Express ErrorFrequently asked questions
Why does return res.json help?
Returning from the route or middleware stops that function's execution at the response point. This prevents later code in that function from accidentally reaching another response path.
Does ERR_HTTP_HEADERS_SENT mean I literally set the same header twice?
Not necessarily. The visible failure can occur when a second response operation attempts to modify response headers after the response has already started.
Should I check res.headersSent?
It is useful for diagnosis and for error middleware that must deal with an already started response. It should not replace fixing the duplicate response path itself.
Can middleware cause this error?
Yes. A middleware function can send a response and then incorrectly continue the chain, which allows another handler to attempt a second response.
Related Express debugging
For broader route, middleware, request-data, and server debugging, see the Express debugging guide .