request-debug

raw JSON →
0.2.0 verified Thu Apr 23 auth: no javascript abandoned

request-debug is a Node.js utility library designed to monitor and debug HTTP(S) requests made by the now-deprecated `request` module. It provides an easy way to intercept and log request headers, bodies (for POST), response headers, response bodies (if a callback is provided to `request`), redirects, and authentication challenges. The current stable version is 0.2.0, and due to its dependency on the unmaintained `request` library, its release cadence is effectively none, making it an abandoned project. Its key differentiator was its tight integration with `request`, patching the module instance directly to emit detailed event data for each request lifecycle stage, either to stderr or via a user-defined callback function.

error Error: Cannot find module 'request'
cause The `request` package, a peer dependency, is not installed.
fix
Install the request module: npm install request
error TypeError: require(...) is not a function
cause Attempted to use ES module `import` syntax or improperly destructured the CJS export, or `request` was not passed correctly.
fix
Ensure you are using require('request-debug')(request); (CommonJS syntax) and that the request module instance is correctly passed as the first argument.
error TypeError: request.stopDebugging is not a function
cause Called `request.stopDebugging()` before `request-debug` was initialized for the `request` instance.
fix
Initialize request-debug with require('request-debug')(request); before attempting to call request.stopDebugging().
breaking The `request-debug` library is built exclusively for and directly depends on the `request` module, which has been officially deprecated and is no longer maintained. This means `request-debug` is also effectively abandoned and may have security vulnerabilities inherited from `request`.
fix Migrate to modern HTTP clients like `node-fetch`, `axios`, or Node.js's built-in `http`/`https` modules, and use their respective debugging or interceptor mechanisms. This library should not be used in new projects.
gotcha This module is CommonJS-only. It cannot be directly imported using ES module `import` syntax (`import requestDebug from 'request-debug'`).
fix Always use `const requestDebug = require('request-debug');` to import the module.
gotcha The `request.stopDebugging()` function only becomes available on the `request` object after `require('request-debug')(request)` has been called. Attempting to call it before enabling debugging will result in a `TypeError`.
fix Ensure `request-debug` is initialized and attached to the `request` instance before attempting to call `request.stopDebugging()`.
gotcha Response bodies (`data.body` in the response event) are only buffered and made available by `request-debug` if the original call to `request` included a callback function. If `request` is used in a streaming or promise-based manner without a callback, the response body may be absent from debug data.
fix When using `request-debug` to inspect response bodies, ensure that the `request` call includes a callback function to buffer the response. Alternatively, consider using the stream `data` events directly on the `request` object for debugging streamed responses.
npm install request-debug
yarn add request-debug
pnpm add request-debug

This example demonstrates how to enable `request-debug` and make a sample HTTP request with basic authentication to an external service (`httpbin.org`), showing how the debug events are logged to the console (stderr by default). It includes a commented-out custom handler example.

const request = require('request');
const util = require('util');

// Enable request-debug, sending output to stderr by default
require('request-debug')(request);

// Alternatively, provide a custom handler function:
// require('request-debug')(request, function(type, data, r) {
//   console.error(`[${type.toUpperCase()}] debugId: ${data.debugId}`);
//   console.error(util.inspect(data, { colors: true, depth: 3 }));
// });

console.log('Making a debuggable HTTP request...');

request({
    uri  : 'https://httpbin.org/digest-auth/auth/user/pass',
    auth : {
        user : 'user',
        pass : 'pass',
        sendImmediately : false
    },
    // IMPORTANT: rejectUnauthorized is only for example purposes to ignore self-signed certs
    // Do NOT use in production with untrusted certs.
    rejectUnauthorized : process.env.NODE_ENV !== 'production' ? false : true,
    headers: {
        'User-Agent': 'request-debug-example'
    }
}, function(err, res, body) {
    if (err) {
        console.error('Request failed:', err.message);
    } else {
        console.log('REQUEST RESULTS:');
        console.log('  Status Code:', res.statusCode);
        console.log('  Body:', body.substring(0, 100) + '...');
    }
    // Stop debugging once the process is complete (optional)
    if (request.stopDebugging) {
        request.stopDebugging();
    }
});