Registry / http-networking / request-spy

request-spy

JSON →
library0.0.10jsnpmunverified

request-spy is a Node.js utility designed to intercept and report on all outgoing HTTP/HTTPS requests made within a Node.js application. It operates by patching Node.js's internal request mechanisms, allowing users to log errors, monitor hostnames, paths, methods, HTTP status codes, and the request duration. The package is currently at version 0.0.10. Given its last known update around 2017 (indicated by the README copyright), it appears to be either in a long-term maintenance state or, more likely, abandoned, suggesting an infrequent or non-existent release cadence. Its primary differentiator is its simple, global interception mechanism, which can be useful for debugging or basic metrics collection without modifying individual request calls. However, its age may present significant compatibility challenges with newer Node.js runtime versions, especially concerning modern module systems like ESM or the `fetch` API.

npm install request-spy
INSTALL
IMPORT
SIG · REQUEST-SPY
R
request-spy
http-networkingjavascriptv0.0.10
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

requestSpy
const requestSpy = require('request-spy');
import requestSpy from 'request-spy';
This package is CommonJS only. Direct ESM import is not supported.
requestSpy.spy
const requestSpy = require('request-spy'); requestSpy.spy((error, requestData) => { /* ... */ });
import { spy } from 'request-spy';
The `spy` function is a method of the default CommonJS export, not a named export.
requestSpy.restore
const requestSpy = require('request-spy'); requestSpy.restore();
import { restore } from 'request-spy';
The `restore` function is a method of the default CommonJS export, not a named export.

This quickstart demonstrates how to initialize `request-spy` to intercept all outgoing HTTP requests, log their details, and then restore the original Node.js behavior after a short period. It includes an example HTTP request to an external API to trigger the spy.

const requestSpy = require('request-spy'); const http = require('http'); // For making a sample outgoing request console.log('Starting request-spy example...'); // Spy on all outgoing requests requestSpy.spy((error, requestData) => { if (error) { console.error('⚠️ Socket error during outgoing request:', error.message); } else { console.log('✨ Outgoing Request Spied:'); console.log(` Hostname: ${requestData.hostname}`); console.log(` Path: ${requestData.path}`); console.log(` Method: ${requestData.method}`); if (requestData.statusCode) { console.log(` Status Code: ${requestData.statusCode}`); } console.log(` Request Time: ${requestData.requestTime}ms`); } }); // Make a sample outgoing request to trigger the spy const options = { hostname: 'jsonplaceholder.typicode.com', port: 80, path: '/todos/1', method: 'GET', }; const req = http.request(options, (res) => { console.log(`Response status from external API: ${res.statusCode}`); res.on('data', () => {}); // Consume response data to ensure 'end' event fires res.on('end', () => { console.log('Request to jsonplaceholder.typicode.com completed.'); }); }); req.on('error', (e) => { console.error(`Problem with the sample request: ${e.message}`); }); req.end(); // Restore the original http/https modules after a short delay setTimeout(() => { requestSpy.restore(); console.log('request-spy restored. No more requests will be spied upon.'); }, 2000);
Debug
Known issues
breakingDue to its age and reliance on patching Node.js internals (specifically `http.request` and `https.request`), `request-spy` may not function correctly or might cause unexpected behavior with newer Node.js versions (especially v14+). Significant changes in Node.js's networking stack or module system could break its functionality.
fix
Consider using modern alternatives like `nock` (for mocking) or `axios-debug-log` / custom interceptors for libraries like `axios` or `node-fetch` if you need request logging or interception. For general observability, integrate with APM tools.
affects: >=0.0.10
gotchaThis package is CommonJS-only. Attempting to use `import requestSpy from 'request-spy';` in an ESM module will result in a runtime error or an unexpected module structure. It relies on the `require` syntax.
fix
Always use `const requestSpy = require('request-spy');` when importing this package in a CommonJS context. If your project is ESM, you might need to use `createRequire` from the `module` module or consider a different library.
affects: >=0.0.1
gotcharequest-spy only intercepts requests made via Node.js's built-in `http` and `https` modules. It will not intercept requests made using newer APIs like `fetch` (if used with a Node.js polyfill or native `fetch` support) or other third-party networking libraries that do not internally rely on the standard `http/https` modules.
fix
Verify that your application's outgoing requests are indeed using the `http` or `https` modules. For broader interception, a proxy-based solution or dedicated APM/observability tools might be more effective.
affects: >=0.0.1
gotchaPatching global Node.js internals can lead to conflicts with other libraries that perform similar patching or interfere with debugging tools. This can result in unpredictable behavior or incorrect reporting.
fix
Avoid using `request-spy` alongside other global patching libraries. Test thoroughly in your environment. If conflicts arise, consider isolating the spying functionality to specific parts of your application or using less intrusive methods.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: requestSpy.spy is not a function
Attempting to import `spy` or `restore` as named exports from the package, or using an ESM `import` statement in a CJS context.
fix
Ensure you are using `const requestSpy = require('request-spy');` and then calling `requestSpy.spy(...)` or `requestSpy.restore()`.
Error: Cannot find module 'request-spy'
The package is not installed or the Node.js module resolution path is incorrect. Also, an ESM `import` statement trying to resolve a CJS module might sometimes manifest this way in specific build setups.
fix
Run `npm install request-spy` to ensure the package is installed. If using ESM, confirm that `createRequire` or similar compatibility layers are correctly configured, or revert to CommonJS.
(node:XXXXX) DeprecationWarning: [...]
Using an older library that patches deprecated internal Node.js APIs or relies on behavior that has been marked for deprecation in newer Node.js versions.
fix
This warning indicates potential future breakage. The fix would involve updating or replacing `request-spy` with a more current alternative compatible with your Node.js version, as `request-spy` itself is no longer maintained.
Upgrade
Version history
0.0.10latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
16
OpenAI (training)
2
Resources
request-spy — npm install request-spy · libregistry