# @orrisroot/inject-loader

> A Webpack loader for injecting code into modules via their dependencies

Latest version **5.1.0** (published 2026-05-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install @orrisroot/inject-loader
pnpm add @orrisroot/inject-loader
yarn add @orrisroot/inject-loader
bun add @orrisroot/inject-loader
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 5.1.0 |
| Published | 2026-05-20 |
| First published | 2023-08-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 35.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Justin Morris |
| Maintainers | orrisroot |
| Keywords | webpack, testing, loader, webpack-loader, inject, mock, mocking |

## Links

- npm: https://www.npmjs.com/package/@orrisroot/inject-loader
- Repository: https://github.com/orrisroot/inject-loader
- Homepage: https://github.com/orrisroot/inject-loader#readme
- Issues: https://github.com/orrisroot/inject-loader/issues
- npm.io page: https://npm.io/package/@orrisroot/inject-loader

## Dependencies (1)

- [@babel/core](https://npm.io/package/@babel/core.md) ^7

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 5.1.0 (latest) — 2026-05-20
- 5.0.0 — 2023-08-21

## README

# 💉📦 inject-loader

A Webpack loader for injecting code into modules via their dependencies

[![npm](https://img.shields.io/npm/v/@orrisroot/inject-loader.svg)](https://www.npmjs.com/package/@orrisroot/inject-loader)
[![Downloads](https://img.shields.io/npm/dw/@orrisroot/inject-loader.svg)](https://www.npmjs.com/package/@orrisroot/inject-loader)

***

## Why

This is particularly useful for writing tests where mocking things inside your module-under-test is sometimes necessary before execution.

`inject-loader` was inspired by, and builds upon ideas introduced in [jauco/webpack-injectable](https://github.com/jauco/webpack-injectable).

This library has been forked from [inject-loader](https://github.com/plasticine/inject-loader) and renamed to [@orrisroot/inject-loader](https://github.com/orrisroot/inject-loader) for continued maintenance.

## Usage

[Documentation: Using loaders](http://webpack.github.io/docs/using-loaders.html)

Use the inject loader by adding the `@orrisroot/inject-loader!` [inline loader](https://webpack.js.org/concepts/loaders/#inline) when you use `require`, this will return a function that can used in test code to modify the injected module.

By default all `require` statements in an injected module will be altered to be replaced with an injector, though if a replacement it not specified the default values will be used.

## Examples

Given some code in a module like this:

```javascript
// MyStore.js

var Dispatcher = require('lib/dispatcher');
var EventEmitter = require('events').EventEmitter;
var handleAction = require('lib/handle_action');

Dispatcher.register(handleAction, 'MyStore');
```

You can manipulate it’s dependencies when you come to write tests as follows:

```javascript
// If no flags are provided when using the loader then
// all require statements will be wrapped in an injector
MyModuleInjector = require('@orrisroot/inject-loader!MyStore')
MyModule = MyModuleInjector({
  'lib/dispatcher': DispatcherMock,
  'events': EventsMock,
  'lib/handle_action': HandleActionMock
})
```

There are a few examples of complete test setups for Webpack 5 in the [`example`](./example) folder.

## License

The source code is licensed MIT. see [`LICENSE`](./LICENSE).

---
_Source: https://npm.io/package/@orrisroot/inject-loader · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
