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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
inject-loader
✓ import MyModuleInjector from 'inject-loader!./MyModule';
✗ const inject = require('inject-loader'); const MyModuleInjector = inject('./MyModule');
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.
inject-loader (CommonJS)
✓ const MyModuleInjector = require('inject-loader!./MyModule');
✗ const MyModuleInjector = require('inject-loader')( './MyModule' );
When using CommonJS, still use the inline loader syntax in the require path.
injector function
✓ const MyModule = MyModuleInjector({ 'lib/dispatcher': DispatcherMock, 'events': EventsMock, 'lib/handle_action': HandleActionMock });
✗ const MyModule = MyModuleInjector.inject({ 'lib/dispatcher': DispatcherMock });
The loader returns a function that accepts an object mapping dependency paths to mock modules.
Shows how to require a module via inject-loader inline and inject mock dependencies.
// Example test using inject-loader with Jest
const MyModuleInjector = require('inject-loader!./MyModule');
const DispatcherMock = { register: jest.fn() };
const EventsMock = { EventEmitter: jest.fn() };
const HandleActionMock = jest.fn();
const MyModule = MyModuleInjector({
'lib/dispatcher': DispatcherMock,
'events': EventsMock,
'lib/handle_action': HandleActionMock
});
// Now MyModule uses the mocked dependencies
DispatcherMock.register.mock.calls.length; // 1
Errors
Common errors & fixes
TypeError: inject_loader is not a function
Using inject-loader as a regular module instead of an inline loader.
fixUse require('inject-loader!./MyModule') instead of require('inject-loader')(...). Module not found: Error: Can't resolve 'inject-loader'
inject-loader not installed or not in node_modules.
fixRun 'npm install --save-dev inject-loader'.
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
Webpack 4 warns about dynamic requires; inject-loader uses dynamic require internally.
fixThis warning is expected and usually harmless. Suppress with webpack config stats: 'warningsFilter: /Critical dependency/'.
Inject loader only works with CommonJS modules (module.exports/require).
Using ES module syntax (import/export) in the injected module.
fixEnsure the module under test uses CommonJS require/module.exports, or use a babel transform to convert.
Audit
Dependencies
webpackrequiredpeer dependency required to function as a Webpack loader