# nestjs-multi-provider

> This package patches NestJS with the multi provider pattern.

Latest version **0.3.4** (published 2026-07-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-multi-provider
pnpm add nestjs-multi-provider
yarn add nestjs-multi-provider
bun add nestjs-multi-provider
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.3.4 |
| Published | 2026-07-03 |
| First published | 2023-11-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 113.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Helveg |
| Maintainers | helveg |
| Keywords | nestjs, multi-provider, multi provider |

## Links

- npm: https://www.npmjs.com/package/nestjs-multi-provider
- Repository: https://github.com/Helveg/nestjs-multi-provider
- Homepage: https://github.com/Helveg/nestjs-multi-provider#readme
- Issues: https://github.com/Helveg/nestjs-multi-provider/issues
- npm.io page: https://npm.io/package/nestjs-multi-provider

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 0.3.4 (latest) — 2026-07-03
- 0.3.3 — 2025-11-02
- 0.3.2 — 2024-04-11
- 0.3.1 — 2024-04-11
- 0.3.0 — 2023-11-12
- 0.2.0 — 2023-11-08
- 0.1.5 — 2023-11-08
- 0.1.4 — 2023-11-05
- 0.1.3 — 2023-11-05
- 0.1.2 — 2023-11-04
- 0.1.1 — 2023-11-04
- 0.1.0 — 2023-11-04

## README

# NestJS patch for multi provider pattern

This package patches the [Nest.js](https://nestjs.com/) `Module` decorator so that you can
somewhat straightforwardly use the multi-provider pattern. This package lets you specify
the `multi` flag to providers. You can then collect these providers into an array using
the `collect` function in the `imports` attribute of a target module, and inject the
collected array of provided values anywhere in the target module.

## Installation

```
npm install nestjs-multi-provider
```

## Usage

A full example is available [here](https://github.com/Helveg/nestjs-multi-provider/tree/main/example).

On the first line of your root module file (most likely `src/app.module.ts`), import the
package to activate the patch:

```typescript
import 'nestjs-multi-provider';
```

**Note:** This package monkey-patches the `Module` decorator, if for some reason you
can't, see [the failsafe method](#Using-the-failsafe-decorator).

### 1. Provide

Simply use any of the provider patterns NestJS supports, and add the `multi` property. You
can do this from any module, any amount of times. All the providers will ultimately be
available under the same token as an array of provided values.

```typescript
@Module({
    providers: [
        {provide: SOME_TOKEN, useClass: SomeService, multi: true},
        {provide: SOME_TOKEN, useClass: SomeServiceWithDeps, multi: true, standalone: false},
    ]
})
```

**Important:** If your provider has dependencies, you must set `standalone` to `false`.

### 2. Collect

To access all your providers, use the `collect` function:

```typescript
@Module({
    imports: [collect(SOME_TOKEN)]
})
export class CollectingModule {}
```

### 3. Inject

You can now inject your array of providers anywhere inside of `CollectingModule`:

```typescript
export class SomeServiceInCollectingModule {
    constructor(@Inject(SOME_TOKEN) collection: any[]) {
        console.log("Collected:", collection)
        // This will print `Collected: [SomeService {}, SomeServiceWithDeps {}]`
    }
}
```

## Using the failsafe decorator

Instead of using the patched `Module` decorator, you can also decorate any modules that
provide multi-providers with the `ModuleWithMulti` decorator:

```typescript
import { ModuleWithMulti } from "nestjs-multi-provider";

@ModuleWithMulti({
    providers: [{provide: "something", useValue: 1, multi: true}]
})
export class MyModule {}
```

## During testing

I have noticed during testing that the monkey patch doesn't always function because
certain statements may be hoisted above the patch. You can overcome this by creating
a statement that itself will be hoisted. For example, for Jest:

```
import { ModuleWithMulti } from "nestjs-multi-provider";

jest.mock('@nestjs/common', () => {
  const nestjsCommon = jest.requireActual('@nestjs/common');
  return {
    ...nestjsCommon,
    Module: ModuleWithMulti,
  };
});
```

## Important notes

* Your providers are not available unless you use `collect`.
* `collect` creates a `DynamicModule` and therefor must be placed in the `imports` property.
* Your providers are by default considered `standalone` (not having any dependencies),
  and are not actually provided by the module they are declared in, but by the collecting
  `DynamicModule`. This is done to avoid creating needless dependencies between modules.
* If your provider has dependencies, set `standalone` to `false` to have access to the
  declaring module's providers. Doing this will result in a `forwardRef` of the declaring
  module by the collecting module.
* Multi providers are not supported in `DynamicModule`s.

---
_Source: https://npm.io/package/nestjs-multi-provider · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
