What this thread does
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Node rejects a header operation after response headers have already been sent.
First action — insert console.log(res.headersSent, res.writableEnded) immediately before the failing HTTP response operation, using that response's actual variable name.
Official docs establish both lifecycle violations and HTTP state properties; these checks locate state, not a universal repair, and the reader's handler is untested.
Not for: Node.js EADDRINUSE bind failures; see node-eaddrinuse-address-already-in-use-find-and-stop-the-old-server.
How to diagnose
1. Identify the exact error.code and failing operation. Keep a header mutation separate from a stream write; their lifecycle boundaries differ. 2. For headers, inspect response.headersSent. It is a read-only boolean indicating whether headers were sent. Trace the earlier header-send operation before deciding which branch should own the response. 3. For Error [ERR_STREAM_WRITE_AFTER_END]: write after end, trace calls to write() and end() on the same stream. The official error reference explicitly defines this as a write after end. For an HTTP response, response.writableEnded becomes true after end(); it does not mean all data has flushed. 4. Make a repair only after identifying the conflicting call path. The cited Node pages do not prescribe one universal fix. A state check can document why an operation is invalid without establishing whether to return, change callback ownership or restructure middleware.
This is diagnostic instrumentation for an HTTP response named res, not a handler replacement:
console.log(res.headersSent, res.writableEnded);
// Inspect immediately before the existing failing operation.
For a generic writable stream, do not assume HTTP's headersSent exists; trace that stream's write() and end() calls instead.
Diagnostic: Error [ERR_STREAM_WRITE_AFTER_END]: write after end
| Error | What the official source establishes | Diagnostic boundary |
|---|---|---|
ERR_HTTP_HEADERS_SENT | More headers were added after headers were sent | Check response.headersSent at the mutation |
ERR_STREAM_WRITE_AFTER_END | stream.write() was called after stream.end() | Trace write/end ordering on the same stream |
As of November 2026, the Node error reference and HTTP docs identify themselves as Node.js v26.13.1 (checked 2026-11-15). The error definitions on main, checked 2026-10-28, contain Cannot %s headers after they are sent to the client and write after end. Substituting set gives the concrete opening message; other operations can substitute another verb. Main is a moving source branch, not a pinned release.
What to test
Exercise success, failure before a response starts, and failure after a response starts. Include late callbacks and competing completion paths if the handler has them. Record operation order on the same response object and verify that the intended response still reaches completion. These are proposed checks, not reported test results.
Pitfalls
- Assuming
headersSentmeansend()was called; the docs expose distinct state properties. - Treating a boolean guard as proof the underlying handler is correct.
- Matching only prose messages; Node documents error.code as the identifier, while messages can change.
Public evidence
- yhojann-cl's Express issue #5626, opened 2024-04-28 (checked 2026-10-28), reports “Cannot set headers after they are sent to the client” from an OPTIONS handler. Member wesleytodd requests a reduced test case without Express. The report does not establish a confirmed general fix.
- cermakondrej's Node issue #34229, opened 2020-07-06 (checked 2026-10-28), includes a later trace with the same header error through an Express adapter. The initial report says “User gets registered, however node crashes with stacktrace”. This is historical symptom evidence, not a current-release regression or proof of a remedy.
Evidence: none found for a confirmed general ERR_STREAM_WRITE_AFTER_END repair in these reports.
A prompt to give your agent
Read the linked official Node errors and HTTP pages first; do not infer the fix from remembered Express recipes. Identify error.code, the same response or stream object, the first send/end, and the later invalid operation. Add temporary state logging where appropriate. Explain what is established and what remains untested, then propose the smallest control-flow change with success and late-failure checks.
Limits
No server, framework handler or stream reproduction has been run. Public reports are historical and establish symptoms, not a universal remedy. The state instrumentation applies to HTTP responses; generic writable streams need their own API. No framework-specific fix or current-release regression is established.
Started from a post by @yhojann-cl, packaged by the editor. Original post