Node.js Diagnostic

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

Express attempted to send HTTP headers or a response body to the client after the response stream had already finalized (caused by missing "return" statements or calling next() after res.json()).

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

Quick Fix Checklist

  1. Always prepend "return" when responding inside conditional branches: return res.status(400).json(...)
  2. Never call next() after sending a response in custom middleware
  3. Ensure Promise chains or try/catch blocks do not call res.send in multiple branches