{"id":18746,"library":"require-middleware","title":"require-middleware","description":"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.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/adamterlson/require-middleware","tags":["javascript","node","js","require","middleware"],"install":[{"cmd":"npm install require-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add require-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add require-middleware","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"ESM-style import preferred; CJS destructure works but ESM is recommended.","wrong":"const wrap = require('require-middleware').wrap","symbol":"wrap","correct":"import { wrap } from 'require-middleware'"},{"note":"Unwraps the require hook; safe to call even if not wrapped.","wrong":"const { unwrap } = require('require-middleware');","symbol":"unwrap","correct":"import { unwrap } from 'require-middleware'"},{"note":"use is a named export, not default.","wrong":"import use from 'require-middleware';","symbol":"use","correct":"import { use } from 'require-middleware'"}],"quickstart":{"code":"import { wrap, unwrap, use } from 'require-middleware';\n\n// Define a middleware that logs module loads\nuse((mod) => {\n  console.log(`Loading module: ${mod.filename}`);\n  return mod;\n});\n\n// Wrap require to enable middleware\nwrap();\n\n// Now require a module, middleware will run\nconst fs = require('fs'); // logs: Loading module: /path/to/fs.js\n\n// Optionally unwrap to restore original require\nunwrap();","lang":"typescript","description":"Demonstrates defining a middleware function, wrapping require, and unwrapping."},"warnings":[{"fix":"Call unwrap() before wrap() again, or check if already wrapped.","message":"Only one wrapper can be active at a time; calling wrap() while already wrapped will error.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Register all middleware via use() before calling wrap().","message":"Middleware cannot be added after wrap() is called; use() must be called before wrap()","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Access mod.exports to modify the module's exports.","message":"The middleware receives an object with 'filename' and 'exports', not the module's exports directly. Modify exports via mod.exports = ...","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use path.relative or resolve manually if comparing paths.","message":"Relative require paths may be misaligned; the filename is absolute but the require call may be relative.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Call unwrap() before wrapping again, or conditionally wrap.","cause":"Calling wrap() twice without intermediate unwrap().","error":"Error: Require middleware already wrapped"},{"fix":"Run npm install require-middleware and ensure import/require path is correct.","cause":"Package not installed or import path incorrect.","error":"Error: Cannot find module 'require-middleware'"},{"fix":"Use { use } from 'require-middleware'.","cause":"Incorrect import; likely default import when named export exists.","error":"TypeError: use is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}