0
0
ExpressDebug / FixBeginner · 4 min read

How to Fix 'Headers Already Sent' Error in Express

The 'headers already sent' error in Express happens when you try to send a response after the headers have been sent. To fix it, ensure you call res.send(), res.json(), or res.end() only once and avoid sending multiple responses in the same request handler.
🔍

Why This Happens

This error occurs because HTTP headers must be sent before the response body. In Express, once you send a response using methods like res.send() or res.json(), the headers are sent automatically. If your code tries to send another response or set headers afterward, Node.js throws the 'headers already sent' error.

Common causes include calling res.send() multiple times, calling next() after sending a response, or asynchronous code sending a response twice.

javascript
app.get('/example', (req, res) => {
  res.send('First response');
  res.send('Second response'); // This causes the error
});
Output
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
🔧

The Fix

To fix this error, make sure you send only one response per request. Avoid calling res.send(), res.json(), or res.end() more than once. Also, do not call next() after sending a response unless you want to pass control without sending another response.

Use conditional logic or return statements to stop execution after sending the response.

javascript
app.get('/example', (req, res) => {
  if (someCondition) {
    return res.send('Response sent once');
  }
  res.send('Another response');
});
Output
Response sent once (or 'Another response' depending on condition)
🛡️

Prevention

To avoid this error in the future, follow these best practices:

  • Always return or end the function after sending a response to prevent further code execution.
  • Use return with res.send() or res.json() to stop the handler.
  • Be careful with asynchronous code; ensure callbacks or promises do not send multiple responses.
  • Use linters like ESLint with rules to detect multiple response calls.
  • Structure your code clearly to separate response logic and error handling.
⚠️

Related Errors

Other errors related to response handling in Express include:

  • Cannot set headers after they are sent to the client: Same as 'headers already sent', caused by multiple response attempts.
  • Unhandled promise rejections: When asynchronous errors are not caught, leading to unexpected behavior.
  • Timeout errors: When response is not sent in time, causing the client to disconnect.

Quick fixes involve proper error handling, using try/catch in async functions, and ensuring only one response per request.

Key Takeaways

Send only one response per request to avoid 'headers already sent' errors.
Use return statements immediately after sending a response to stop further code execution.
Avoid calling next() after sending a response unless intentionally passing control.
Be cautious with asynchronous code to prevent multiple response sends.
Use linters and clear code structure to catch and prevent this error early.