Skip to main content
This recipe migrates code from using deprecated private header fields (_headers, _headerNames) on OutgoingMessage.prototype to their public API equivalents.

Deprecation

Node.js deprecated direct access to private header fields. Use the public methods getHeaders() and getRawHeaderNames() instead. See DEP0066 for more details.

Usage

Run this codemod with:
npx codemod nodejs/http-outgoingmessage-headers

Before/After

  const http = require('http');

  function handler(req, res) {
    res.setHeader('content-type', 'application/json');
    res.setHeader('x-custom-header', '42');

    console.log({
-     headers: res._headers,
+     headers: res.getHeaders(),
-     headerNames: res._headerNames,
+     headerNames: res.getRawHeaderNames(),
-     customHeader: res._headers['x-custom-header'],
+     customHeader: res.getHeaders()['x-custom-header'],
-     count: Object.keys(res._headers).length,
+     count: Object.keys(res.getHeaders()).length,
    });

    res.end('Hello World');
  }

  const server = http.createServer(handler);

  server.listen(3000, () => {
    console.log('Server running on port 3000');
  });

What It Does

  • Replaces res._headers with res.getHeaders()
  • Replaces res._headerNames with res.getRawHeaderNames()
  • Updates property access and method calls throughout your codebase
  • Works with ServerResponse and other OutgoingMessage subclasses

Migration Guide

DeprecatedReplacement
res._headersres.getHeaders()
res._headerNamesres.getRawHeaderNames()
Direct access to private fields (prefixed with _) is not part of the public API and can break without notice. Always use public methods.

References

Build docs developers (and LLMs) love