ERR_HTTP_HEADERS_SENT node http_error ai_generated true

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

ID: node/err-http-headers-sent

Also available as: JSON · Markdown
92%Fix Rate
92%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Response already sent but code tries to send again. Missing return after res.send().

generic

Workarounds

  1. 95% success Add return after res.send()/res.json()/res.redirect() to prevent further execution
    if (error) { return res.status(400).json({ error }); }

    Sources: https://expressjs.com/en/api.html#res.send

  2. 88% success Check for multiple middleware calling next() and also sending response
    Use return after res.send()/res.json() to prevent double-response

    Sources: https://expressjs.com/en/guide/writing-middleware.html

Dead Ends

Common approaches that don't work:

  1. Add res.headersSent check before every response 75% fail

    Doesn't fix the control flow bug — just hides it

  2. Wrap in try/catch 80% fail

    The error is a logic bug, not an exception to catch

Error Chain

Frequently confused with: