Registry / http-networking / node-request-interceptor

node-request-interceptor

JSON →
library0.6.3jsnpmunverified

Low-level HTTP/HTTPS/XHR request interception library for Node.js, currently at v0.6.3 (stable). It monkey-patches native modules like http, https, fetch, and XMLHttpRequest to intercept all outgoing requests, allowing custom logic to be applied before responses are sent. Unlike high-level mocking libraries (e.g., nock, sinon), it provides no request matching or routing—just raw interception. It is designed as a building block for tools like MSW. Releases are frequent, with active maintenance and breaking changes in v0.41.0 affecting CJS exports. Ships TypeScript types and supports ESM and CJS.

npm install node-request-interceptor
INSTALL
IMPORT
SIG · NODE-REQUEST-INTER
N
node-request-interceptor
http-networkingjavascriptv0.6.3
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.

RequestInterceptor
import { RequestInterceptor } from 'node-request-interceptor'
const { RequestInterceptor } = require('node-request-interceptor')
ESM-only since v0.41.0? The package is primarily ESM, but CJS require is still supported via default export. Use ESM for best compatibility.
defaultPreset (presets/default)
import withDefaultInterceptors from 'node-request-interceptor/presets/default'
const { default: preset } = require('node-request-interceptor/presets/default')
In CJS, use require('node-request-interceptor/presets/default') directly, not with .default property.
interceptXMLHttpRequest
import { interceptXMLHttpRequest } from 'node-request-interceptor/interceptors/XMLHttpRequest'
import interceptXMLHttpRequest from 'node-request-interceptor/interceptors/XMLHttpRequest'
This is a named export, not default. The path is case-sensitive.

Creates an interceptor with default presets, adds a middleware to mock a specific request, then restores after use.

import { RequestInterceptor } from 'node-request-interceptor'; import withDefaultInterceptors from 'node-request-interceptor/presets/default'; const interceptor = new RequestInterceptor(withDefaultInterceptors); interceptor.use((req) => { if (req.url.href === 'https://api.example.com/data') { return { status: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'mocked' }), }; } // return undefined to bypass }); // Now any request to that URL will be intercepted fetch('https://api.example.com/data').then(res => res.json()).then(console.log); // Later, restore original modules interceptor.restore();
Debug
Known issues
breakingIn v0.41.0, the CJS bundle no longer contains the named `default` export at `/presets/node`. You must use `require('@mswjs/interceptors/presets/node')` directly without `.default`.
fix
For CJS, use `const preset = require('node-request-interceptor/presets/node');` instead of `const { default: preset } = require(...)`.
affects: >=0.41.0
breakingIn v0.40.0, support for `RequestController` with source was added. This may require changes if you rely on the exact handler signature.
fix
Update your middleware to handle the new `RequestController` argument if needed.
affects: >=0.40.0
gotchaThe library intercepts all requests by default, which can impact performance if not properly restored. Always call `interceptor.restore()` after use, especially in test cleanup.
fix
Use `afterEach(() => interceptor.restore())` in tests, or scope the interceptor to a specific lifecycle.
affects: *
gotchaXMLHttpRequest interception only works in environments that emulate a browser (e.g., jsdom). In plain Node.js without jsdom, XMLHttpRequest is not available and will error.
fix
Only use XMLHttpRequest interceptor if you are in a jsdom environment. For Node.js HTTP/HTTPS, use the default presets.
affects: *
deprecatedDirect import from 'node-request-interceptor/interceptors/ClientRequest' is considered low-level; prefer using presets like '/presets/default' for full coverage.
fix
Use `import withDefaultInterceptors from 'node-request-interceptor/presets/default'` instead of manually importing individual interceptors.
affects: *
Errors
Common errors & fixes
Error: Cannot find module 'node-request-interceptor/presets/default'
Wrong import path; the correct path is 'node-request-interceptor/presets/default' (note the 's' in presets).
fix
Use `import withDefaultInterceptors from 'node-request-interceptor/presets/default';`
TypeError: interceptor.use is not a function
You may have imported the wrong export. `RequestInterceptor` is a class, not a function. Ensure you instantiate it with `new RequestInterceptor(...)`.
fix
Correct: `const interceptor = new RequestInterceptor(withDefaultInterceptors);`
ReferenceError: XMLHttpRequest is not defined
XMLHttpRequest interceptor is used in a Node.js environment without jsdom. XMLHttpRequest is only available in browser-like environments.
fix
Remove the XMLHttpRequest interceptor or ensure jsdom is loaded before using it.
Upgrade
Version history
0.6.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources