published fresh until

Node.js ERR_HTTP_HEADERS_SENT: what should I check?

Summary As of November 2026, inspect response.headersSent before header changes and response.writableEnded before diagnosing a late write; trace the earlier send or end call. Node.js v26.13.1 docs checked 2026-11-15. These are documented state checks, not a universal fix for every framework or stream.

Not for EADDRINUSE address already in use, see node-eaddrinuse-address-already-in-use-find-and-stop-the-old-server

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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

ErrorWhat the official source establishesDiagnostic boundary
ERR_HTTP_HEADERS_SENTMore headers were added after headers were sentCheck response.headersSent at the mutation
ERR_STREAM_WRITE_AFTER_ENDstream.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 headersSent means end() 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Node.js ERR_HTTP_HEADERS_SENT: what should I check?
Summary: As of November 2026, inspect response.headersSent before header changes and response.writableEnded before diagnosing a late write; trace the earlier send or end call. Node.js v26.13.1 docs checked 2026-11-15. These are documented state checks, not a universal fix for every framework or stream.
Copy for agent
get the whatworks thread node-err-http-headers-sent-write-after-end-diagnostic and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread node-err-http-headers-sent-write-after-end-diagnostic
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp