Registry / testing / require-middleware

require-middleware

JSON →
library1.0.0jsnpmunverified

Inject middleware into Node.js require() calls before they hit the cache, file system, or native modules. Version 1.0.0 is the initial stable release. It allows intercepting and transforming modules at load time, useful for instrumentation, mocking, or polyfilling. Differentiates by operating at the require level rather than module-level hooks, providing a middleware pipeline similar to Express.

npm install require-middleware
INSTALL
IMPORT
SIG · REQUIRE-MIDDLEWARE
R
require-middleware
testingjavascriptv1.0.0
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.

wrap
import { wrap } from 'require-middleware'
const wrap = require('require-middleware').wrap
ESM-style import preferred; CJS destructure works but ESM is recommended.
unwrap
import { unwrap } from 'require-middleware'
const { unwrap } = require('require-middleware');
Unwraps the require hook; safe to call even if not wrapped.
use
import { use } from 'require-middleware'
import use from 'require-middleware';
use is a named export, not default.

Demonstrates defining a middleware function, wrapping require, and unwrapping.

import { wrap, unwrap, use } from 'require-middleware'; // Define a middleware that logs module loads use((mod) => { console.log(`Loading module: ${mod.filename}`); return mod; }); // Wrap require to enable middleware wrap(); // Now require a module, middleware will run const fs = require('fs'); // logs: Loading module: /path/to/fs.js // Optionally unwrap to restore original require unwrap();
Debug
Known issues
breakingOnly one wrapper can be active at a time; calling wrap() while already wrapped will error.
fix
Call unwrap() before wrap() again, or check if already wrapped.
affects: >=1.0.0
breakingMiddleware cannot be added after wrap() is called; use() must be called before wrap()
fix
Register all middleware via use() before calling wrap().
affects: >=1.0.0
gotchaThe middleware receives an object with 'filename' and 'exports', not the module's exports directly. Modify exports via mod.exports = ...
fix
Access mod.exports to modify the module's exports.
affects: >=1.0.0
gotchaRelative require paths may be misaligned; the filename is absolute but the require call may be relative.
fix
Use path.relative or resolve manually if comparing paths.
affects: >=1.0.0
Errors
Common errors & fixes
Error: Require middleware already wrapped
Calling wrap() twice without intermediate unwrap().
fix
Call unwrap() before wrapping again, or conditionally wrap.
Error: Cannot find module 'require-middleware'
Package not installed or import path incorrect.
fix
Run npm install require-middleware and ensure import/require path is correct.
TypeError: use is not a function
Incorrect import; likely default import when named export exists.
fix
Use { use } from 'require-middleware'.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
18 hits · last 30 days
node
14
OpenAI (training)
2
Resources
require-middleware — npm install require-middleware · libregistry