{"id":20138,"library":"inject-loader","title":"inject-loader","description":"A Webpack loader for injecting mock dependencies into modules under test. Current stable version 4.0.1, maintained steadily. It wraps all require statements in a module so you can substitute them during testing. Unlike proxyquire or rewire, it works as a Webpack inline loader, making it seamless in Webpack-based projects. Supports Webpack 1 through 4. Particularly useful for unit testing where you need to mock internal dependencies of a module before it executes.","status":"active","version":"4.0.1","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/plasticine/inject-loader","tags":["javascript","webpack","testing","loader","webpack-loader","inject","mock","mocking"],"install":[{"cmd":"npm install inject-loader","lang":"bash","label":"npm"},{"cmd":"yarn add inject-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add inject-loader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency required to function as a Webpack loader","package":"webpack","optional":false}],"imports":[{"note":"inject-loader is used as an inline Webpack loader, not a regular module. The correct usage is to prefix the module path with 'inject-loader!' in the require/import statement.","wrong":"const inject = require('inject-loader'); const MyModuleInjector = inject('./MyModule');","symbol":"inject-loader","correct":"import MyModuleInjector from 'inject-loader!./MyModule';"},{"note":"When using CommonJS, still use the inline loader syntax in the require path.","wrong":"const MyModuleInjector = require('inject-loader')( './MyModule' );","symbol":"inject-loader (CommonJS)","correct":"const MyModuleInjector = require('inject-loader!./MyModule');"},{"note":"The loader returns a function that accepts an object mapping dependency paths to mock modules.","wrong":"const MyModule = MyModuleInjector.inject({ 'lib/dispatcher': DispatcherMock });","symbol":"injector function","correct":"const MyModule = MyModuleInjector({ 'lib/dispatcher': DispatcherMock, 'events': EventsMock, 'lib/handle_action': HandleActionMock });"}],"quickstart":{"code":"// Example test using inject-loader with Jest\nconst MyModuleInjector = require('inject-loader!./MyModule');\nconst DispatcherMock = { register: jest.fn() };\nconst EventsMock = { EventEmitter: jest.fn() };\nconst HandleActionMock = jest.fn();\nconst MyModule = MyModuleInjector({\n  'lib/dispatcher': DispatcherMock,\n  'events': EventsMock,\n  'lib/handle_action': HandleActionMock\n});\n// Now MyModule uses the mocked dependencies\nDispatcherMock.register.mock.calls.length; // 1","lang":"javascript","description":"Shows how to require a module via inject-loader inline and inject mock dependencies."},"warnings":[{"fix":"Use 'require(\"inject-loader!./module\")' instead of 'require(\"inject-loader\").(./module)'.","message":"You must use the inline loader syntax 'inject-loader!./module' in your require/import statements. Using inject-loader as a regular module and calling it as a function will not work.","severity":"gotcha","affected_versions":">=4.0"},{"fix":"Upgrade to Webpack 3 or 4 for official support.","message":"Webpack 1 and 2 support is deprecated. The loader still works but no longer actively tested.","severity":"deprecated","affected_versions":">=4.0"},{"fix":"Check loader options or use flags as described in the example folder: require('inject-loader?exclude=events!./module').","message":"The loader wraps all require statements by default. If you want to exclude certain dependencies, you must provide flags (not documented in readme).","severity":"gotcha","affected_versions":">=4.0"},{"fix":"Call the result directly: const module = require('inject-loader!./module')({...}) instead of require('inject-loader!./module').inject({...}).","message":"In v4, the loader now returns a single injector function instead of an object with inject method.","severity":"breaking","affected_versions":">=4.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Use require('inject-loader!./MyModule') instead of require('inject-loader')(...).","cause":"Using inject-loader as a regular module instead of an inline loader.","error":"TypeError: inject_loader is not a function"},{"fix":"Run 'npm install --save-dev inject-loader'.","cause":"inject-loader not installed or not in node_modules.","error":"Module not found: Error: Can't resolve 'inject-loader'"},{"fix":"This warning is expected and usually harmless. Suppress with webpack config stats: 'warningsFilter: /Critical dependency/'.","cause":"Webpack 4 warns about dynamic requires; inject-loader uses dynamic require internally.","error":"Critical dependency: require function is used in a way in which dependencies cannot be statically extracted"},{"fix":"Ensure the module under test uses CommonJS require/module.exports, or use a babel transform to convert.","cause":"Using ES module syntax (import/export) in the injected module.","error":"Inject loader only works with CommonJS modules (module.exports/require)."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}