Debug HTTP/HTTPS Requests
debug-http is a minimalist Node.js utility designed to intercept and log all outgoing HTTP and HTTPS requests made within an application. It achieves this by patching Node.js's built-in `http` and `https` modules to provide visibility into request details like URL, method, and headers. The package is currently at version 1.1.0 and has not seen updates in nearly a decade, indicating it is no longer actively maintained. Its primary function is a simple, global request handler that outputs request information to the console by default, though it allows for a custom handler function. Due to its age, it exclusively supports CommonJS module loading and does not officially support modern JavaScript features like ESM or TypeScript. Alternatives like `debug` or `request-debug` offer more robust and actively maintained debugging capabilities, especially for specific HTTP client libraries or more granular control over debug output.
Common errors
-
TypeError: debugHttp is not a function
cause This error often occurs when attempting to destructure the `require` result, or if using `require('debug-http').debugHttp` in CommonJS. The package directly exports the function, not an object with a named export.fixCorrect the CommonJS import to `const debugHttp = require('debug-http');` and then call `debugHttp();`. -
ERR_REQUIRE_ESM: require() of ES Module ... not supported
cause This package is a CommonJS module and cannot be directly `require()`d from an ES Module context without specific Node.js configuration or dynamic imports.fixIf your project is ESM, either switch back to CommonJS, configure Node.js to handle CJS in ESM (e.g., `"type": "commonjs"` in `package.json`), or use a dynamic import: `const debugHttpModule = await import('debug-http'); debugHttpModule.default();` (though for `debug-http`, it's `debugHttpModule()`).
Warnings
- breaking This package globally patches Node.js's built-in `http` and `https` modules. This can lead to unexpected behavior or conflicts if other modules in your application also attempt to patch these core modules or rely on their unpatched behavior. Use with caution in larger applications.
- gotcha The `debug-http` package is unmaintained, with its last update over 10 years ago. It may not be compatible with newer Node.js versions, modern HTTP features, or security best practices, and will not receive updates for bugs or vulnerabilities.
- gotcha The package only supports CommonJS module loading (`require`). Attempting to `import debugHttp from 'debug-http'` in an ESM context will result in an error or undefined behavior due to the lack of an explicit ESM export.
Install
-
npm install debug-http -
yarn add debug-http -
pnpm add debug-http
Imports
- debugHttp
const debugHttp = require('debug-http').debugHttp;import debugHttp from 'debug-http'
- debugHttp
const debugHttp = require('debug-http');
Quickstart
const debugHttp = require('debug-http');
const http = require('http');
const https = require('https');
// Enable HTTP/HTTPS request debugging globally
debugHttp();
console.log('Making an HTTP GET request to google.com...');
http.get('http://google.com', (res) => {
console.log(`HTTP GET to google.com status: ${res.statusCode}`);
res.resume(); // Consume response data to free up memory
}).on('error', (e) => {
console.error(`HTTP GET error: ${e.message}`);
});
console.log('Making an HTTPS GET request to github.com...');
https.get('https://github.com', (res) => {
console.log(`HTTPS GET to github.com status: ${res.statusCode}`);
res.resume(); // Consume response data
}).on('error', (e) => {
console.error(`HTTPS GET error: ${e.message}`);
});
// Example with a custom handler function
// debugHttp((type, data) => {
// console.log(`[${type.toUpperCase()}] Custom Handler:`, data.url || data.path);
// });