{"id":18616,"library":"node-request-interceptor","title":"node-request-interceptor","description":"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.","status":"active","version":"0.6.3","language":"javascript","source_language":"en","source_url":"https://github.com/mswjs/node-request-interceptor","tags":["javascript","request","intercept","http","https","xmlhttprequest","xhr","fetch","low-level","typescript"],"install":[{"cmd":"npm install node-request-interceptor","lang":"bash","label":"npm"},{"cmd":"yarn add node-request-interceptor","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-request-interceptor","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"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.","wrong":"const { RequestInterceptor } = require('node-request-interceptor')","symbol":"RequestInterceptor","correct":"import { RequestInterceptor } from 'node-request-interceptor'"},{"note":"In CJS, use require('node-request-interceptor/presets/default') directly, not with .default property.","wrong":"const { default: preset } = require('node-request-interceptor/presets/default')","symbol":"defaultPreset (presets/default)","correct":"import withDefaultInterceptors from 'node-request-interceptor/presets/default'"},{"note":"This is a named export, not default. The path is case-sensitive.","wrong":"import interceptXMLHttpRequest from 'node-request-interceptor/interceptors/XMLHttpRequest'","symbol":"interceptXMLHttpRequest","correct":"import { interceptXMLHttpRequest } from 'node-request-interceptor/interceptors/XMLHttpRequest'"}],"quickstart":{"code":"import { RequestInterceptor } from 'node-request-interceptor';\nimport withDefaultInterceptors from 'node-request-interceptor/presets/default';\n\nconst interceptor = new RequestInterceptor(withDefaultInterceptors);\n\ninterceptor.use((req) => {\n  if (req.url.href === 'https://api.example.com/data') {\n    return {\n      status: 200,\n      headers: { 'Content-Type': 'application/json' },\n      body: JSON.stringify({ message: 'mocked' }),\n    };\n  }\n  // return undefined to bypass\n});\n\n// Now any request to that URL will be intercepted\nfetch('https://api.example.com/data').then(res => res.json()).then(console.log);\n\n// Later, restore original modules\ninterceptor.restore();\n","lang":"typescript","description":"Creates an interceptor with default presets, adds a middleware to mock a specific request, then restores after use."},"warnings":[{"fix":"For CJS, use `const preset = require('node-request-interceptor/presets/node');` instead of `const { default: preset } = require(...)`.","message":"In 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`.","severity":"breaking","affected_versions":">=0.41.0"},{"fix":"Update your middleware to handle the new `RequestController` argument if needed.","message":"In v0.40.0, support for `RequestController` with source was added. This may require changes if you rely on the exact handler signature.","severity":"breaking","affected_versions":">=0.40.0"},{"fix":"Use `afterEach(() => interceptor.restore())` in tests, or scope the interceptor to a specific lifecycle.","message":"The library intercepts all requests by default, which can impact performance if not properly restored. Always call `interceptor.restore()` after use, especially in test cleanup.","severity":"gotcha","affected_versions":"*"},{"fix":"Only use XMLHttpRequest interceptor if you are in a jsdom environment. For Node.js HTTP/HTTPS, use the default presets.","message":"XMLHttpRequest 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.","severity":"gotcha","affected_versions":"*"},{"fix":"Use `import withDefaultInterceptors from 'node-request-interceptor/presets/default'` instead of manually importing individual interceptors.","message":"Direct import from 'node-request-interceptor/interceptors/ClientRequest' is considered low-level; prefer using presets like '/presets/default' for full coverage.","severity":"deprecated","affected_versions":"*"}],"env_vars":null,"search_vec":"'activ':79 'affect':86 'allow':35 'appli':40 'block':70 'break':82 'build':69 'chang':83 'cjs':87,96 'current':14 'custom':36 'design':66 'e.g':51 'esm':94 'export':88 'fetch':27,104 'frequent':77 'high':47 'high-level':46 'http':25,100 'http/https/xhr':8 'https':26,101 'intercept':10,31,63,99 'interceptor':4 'javascript':97 'level':7,48,107 'librari':11,50 'like':24,73 'logic':37 'low':6,106 'low-level':5,105 'mainten':80 'match':58 'mock':49 'modul':23 'monkey':20 'monkey-patch':19 'msw':74 'nativ':22 'nock':52 'node':2 'node-request-interceptor':1 'node.js':13 'outgo':33 'patch':21 'provid':55 'raw':62 'releas':75 'request':3,9,34,57,98 'respons':42 'rout':60 'sent':44 'ship':89 'sinon':53 'stabl':17 'support':93 'tool':72 'type':91 'typescript':90,108 'unlik':45 'v0.41.0':85 'v0.6.3':16 'xhr':103 'xmlhttprequest':29,102","created_at":"2026-04-25T07:39:14.678009+00:00","updated_at":"2026-04-25T07:39:14.678009+00:00","problems":[{"fix":"Use `import withDefaultInterceptors from 'node-request-interceptor/presets/default';`","cause":"Wrong import path; the correct path is 'node-request-interceptor/presets/default' (note the 's' in presets).","error":"Error: Cannot find module 'node-request-interceptor/presets/default'"},{"fix":"Correct: `const interceptor = new RequestInterceptor(withDefaultInterceptors);`","cause":"You may have imported the wrong export. `RequestInterceptor` is a class, not a function. Ensure you instantiate it with `new RequestInterceptor(...)`.","error":"TypeError: interceptor.use is not a function"},{"fix":"Remove the XMLHttpRequest interceptor or ensure jsdom is loaded before using it.","cause":"XMLHttpRequest interceptor is used in a Node.js environment without jsdom. XMLHttpRequest is only available in browser-like environments.","error":"ReferenceError: XMLHttpRequest is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"","cli_version":null,"type":"library","homepage":null,"github":"https://github.com/mswjs/node-request-interceptor","docs":null,"changelog":null,"pypi":null,"npm":"https://www.npmjs.com/package/node-request-interceptor","openapi_spec":null,"status_page":null,"smithery":null,"categories":["http-networking","testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-17","next_check":"2026-07-24","install_tag":null}}