This recipe ensures HTTP classes like OutgoingMessage, ServerResponse, IncomingMessage, and ClientRequest are properly instantiated with the new keyword.
Deprecation
Node.js deprecated calling HTTP constructors without the new keyword. Classes must be instantiated with new.
See DEP0195 for more details.
Usage
Run this codemod with:
npx codemod nodejs/http-classes-with-new
Before/After
// import { IncomingMessage, ClientRequest } from "node:http";
// const http = require("node:http");
- const message = http.OutgoingMessage();
+ const message = new http.OutgoingMessage();
- const response = http.ServerResponse(socket);
+ const response = new http.ServerResponse(socket);
- const incoming = IncomingMessage(socket);
+ const incoming = new IncomingMessage(socket);
- const request = ClientRequest(options);
+ const request = new ClientRequest(options);
What It Does
- Adds
new keyword before OutgoingMessage(), ServerResponse(), IncomingMessage(), and ClientRequest() calls
- Works with both direct imports and namespace imports (e.g.,
http.OutgoingMessage())
- Handles CommonJS and ESM module patterns
Affected Classes
The following HTTP classes are updated:
OutgoingMessage
ServerResponse
IncomingMessage
ClientRequest
Modern JavaScript requires all classes to be instantiated with the new keyword. This deprecation aligns Node.js HTTP classes with standard JavaScript class syntax.