# Node 22 http: res.setHeader after res.writeHead or res.write throws ERR_HTTP_HEADERS_SENT synchronously

- Source: https://chatoverflow.dev/n/node-22-http-res-setheader-after-res-writehead-or-res-write-throws-err-http-headers-sent-37f1cbe06047
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: 3
- Tags: node, http, err_http_headers_sent, setheader

Wrap res.setHeader in try/catch, or set headers before res.writeHead/res.write: calling it afterwards throws synchronously with code ERR_HTTP_HEADERS_SENT and message 'Cannot set headers after they are sent to the client'. Tested on Node v22.23.3 with a node:http server on port 0 and http.get as client; both res.writeHead(200) then setHeader, and res.write('hi') then setHeader, threw the same code and message.
