# Nest Can't Resolve Dependencies: Every Cause and Fix in NestJS 12

The "Nest can't resolve dependencies of the X (?)" error has six common causes. Read the exact wording, find yours in the table and apply the fix, with NestJS 12 messages and working code.

By Waseem Anjum · Published 2026-11-25 · https://fabwaseem.com/blog/nestjs-cant-resolve-dependencies

> **TL;DR:** "Nest can't resolve dependencies of the X (?)" means Nest found no provider for a constructor argument in the module that declares X. Read which variant you got: a named token usually means a missing export or import, while "appears to be undefined at runtime" points to import type, an interface used as a token, or a circular file import. The fix is almost always one line.

The **Nest can't resolve dependencies** error means NestJS looked for a provider to inject into a constructor and found none in the module that declares the class. It is almost always a one-line fix: export the provider, import its module, switch an `import type` to a normal import, or break a circular import. The exact wording of the message tells you which one you have.

This guide uses the real messages printed by NestJS 12.6 and shows each cause with the smallest working fix, so you can match your error text and move on. NestJS 12 itself is covered in the [NestJS 12 upgrade guide](https://fabwaseem.com/blog/nestjs-12).

## What does "Nest can't resolve dependencies" mean?

It means Nest could not build a provider because one constructor argument has no matching provider in that provider's own module. Every `@Module()` has its own injection context, so a class can only receive providers that are declared in its module or exported by a module it imports. The `(?)` stands for the argument Nest could not resolve, and `[0]` is its position in the constructor.

NestJS 12.6 prints this when a service is missing:

```text
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument EmailService at index [0] is available in the UsersModule module.

Potential solutions:
- Is UsersModule a valid NestJS module?
- If EmailService is a provider, is it part of the current UsersModule?
- If EmailService is exported from a separate @Module, is that module imported within UsersModule?
```

Read three things in it: the **provider** that failed (`UsersService`), the **argument** (`EmailService`, index 0) and the **module** (`UsersModule`). The fix always happens in the module named at the end, or in the module that should supply the argument.

## Which error message do you have?

Nest prints two different variants, and they point at different causes. Check whether the message names the missing token.

![Decision chart: a message that names a token points to exports, imports, self-injection or duplicate @nestjs/core; a message without a token name points to import type, interfaces or circular file imports](https://res.cloudinary.com/wasicloudinary/image/upload/v1795580020/fabwaseem/projects/wvlolggt9shmdfox0mvv.jpg)

| Message says | Most likely cause | Jump to |
| --- | --- | --- |
| `argument EmailService at index [0] is available in the UsersModule module` | Provider not exported, module not imported, or provider missing | [Fix 1](#how-do-i-fix-a-provider-that-is-not-exported-or-imported) |
| `argument "CONFIG" at index [0] ...` (a string in quotes) | Custom token never provided | [Fix 2](#why-does-a-custom-token-fail-to-resolve) |
| `argument Selfie at index [0]` (same name as the provider) | Provider injects itself | [Fix 3](#can-a-provider-inject-itself) |
| `argument ModuleRef at index [0]` | Two copies of `@nestjs/core` | [Fix 4](#why-is-moduleref-the-missing-argument-in-a-monorepo) |
| `at index [0] is available in the current module` and `appears to be undefined at runtime` | `import type`, interface as token, or circular file import | [Fix 5](#what-does-appears-to-be-undefined-at-runtime-mean) |
| `RootTestModule module` | Provider missing from the testing module | [Fix 6](#how-do-i-fix-this-error-in-a-nestjs-unit-test) |

A repository, Prisma or Mongoose model, or a provider from a `forRoot()` module follows the first row: see [the dynamic module section](#why-does-a-repository-or-forroot-provider-not-resolve).

## How do I fix a provider that is not exported or imported?

Export the provider from the module that declares it, then import that module where the provider is needed. A module that lists a service in `providers` keeps it private until it also lists it in `exports`, and importing the module does not change that. This is the cause behind most reports of this error.

![EmailModule exports EmailService and UsersModule imports EmailModule, with the error text shown for the missing export](https://res.cloudinary.com/wasicloudinary/image/upload/v1795580031/fabwaseem/projects/ddc5ri7k6kmc5qa2kox5.jpg)

```ts
// email.module.ts
import { Module } from '@nestjs/common'
import { EmailService } from './email.service'

@Module({
  providers: [EmailService],
  exports: [EmailService], // without this line UsersModule cannot see it
})
export class EmailModule {}

// users.module.ts
import { Module } from '@nestjs/common'
import { EmailModule } from '../email/email.module'
import { UsersService } from './users.service'

@Module({
  imports: [EmailModule],
  providers: [UsersService],
})
export class UsersModule {}
```

Two related mistakes give the same message:

- **The provider's module is not in `imports` at all.** Packages such as JWT or config work the same way: import their module so the services it exports become visible.
- **Declaring the provider twice.** If the same class is also listed in the `providers` of another module, that copy needs its own dependencies in that module, and the error names the second module. Keep one declaration and import its module everywhere else.

If many modules need the same provider, mark its module with `@Global()` and import it once in the root module. The global module must still list the provider in `exports`:

```ts
@Global()
@Module({ providers: [EmailService], exports: [EmailService] })
export class CoreModule {}
```

Use it sparingly, because it hides which module depends on what. The [Nest modules docs](https://docs.nestjs.com/modules) describe both options.

Putting an `@Injectable()` class in the `imports` array is a different error, and a clear one: Nest throws at startup, says the class cannot appear in the `imports` array of a module, and tells you to move it to `providers`.

## Why does a repository or forRoot provider not resolve?

Because a provider created by `forRoot()`, `forFeature()` or `register()` exists only in modules that include that dynamic module, and only if it is exported. A repository, a Prisma service or a Mongoose model is registered by an ORM or database module, so a feature module that injects it must import the module that provides it.

The fix has the same shape as before:

- Import the module that registers the provider into the feature module that injects it, for example the ORM's `forFeature()` call or your own `PrismaModule`.
- Make sure that module lists the provider in `exports`.
- For a dynamic module you write yourself, return `exports` from `forRoot()`, or set `global: true` so other modules see the provider without importing it.

The message looks identical to the missing-export case, so apply the debugging steps below to find which module is missing the import.

## Why does a custom token fail to resolve?

A string or symbol token needs a matching provider with the same token. If a class asks for `@Inject('CONFIG')` and no provider uses `provide: 'CONFIG'` in a reachable module, Nest names the token in quotes:

```text
Nest can't resolve dependencies of the TokenConsumer (?). Please make sure that the argument "CONFIG" at index [0] is available in the TokenModule module.
```

Register the provider and export it the same way as any class provider:

```ts
@Module({
  providers: [{ provide: 'CONFIG', useValue: { region: 'eu' } }],
  exports: ['CONFIG'],
})
export class ConfigModule {}
```

Check for a typo first: `'CONFIG'` and `'Config'` are different tokens. Constants shared between the provider and the `@Inject()` call avoid that. See [custom providers](https://docs.nestjs.com/fundamentals/custom-providers) for the token types Nest accepts.

## Can a provider inject itself?

No. When the missing argument has the same name as the failing provider, the class has itself in its constructor:

```ts
@Injectable()
export class Selfie {
  constructor(private readonly self: Selfie) {} // error: argument Selfie at index [0]
}
```

Remove the self-injection, or split the shared logic into a second provider that both can use.

## Why is ModuleRef the missing argument in a monorepo?

If the missing argument is `ModuleRef`, your project probably loads two copies of `@nestjs/core`. This happens in monorepos when a Nest package such as `@nestjs/bull` ends up with its own nested `@nestjs/core` next to the one at the root. According to the NestJS docs, the fix depends on your package manager:

- **Yarn workspaces:** use the `nohoist` feature so `@nestjs/core` is not hoisted.
- **pnpm workspaces:** declare `@nestjs/core` as a peer dependency of the other package, and set `"dependenciesMeta": { "other-module-name": { "injected": true } }` in the `package.json` of the app that imports it.

Run `pnpm why @nestjs/core` (or `npm ls @nestjs/core`) to confirm you have more than one version before changing anything.

## What does "appears to be undefined at runtime" mean?

It means the class Nest read from the constructor was `undefined` or `Object` when the module was built, so there was no token to look up. NestJS 12.6 prints this variant without a token name:

```text
Nest can't resolve dependencies of the NotifyService (?). Please make sure that the argument at index [0] is available in the current module.

Potential solutions:
- The dependency at index [0] appears to be undefined at runtime
- This commonly occurs when using 'import type' instead of 'import' for injectable classes
```

There are three causes, and they are quick to tell apart:

1. **`import type` on an injectable class.** The import is erased, so the constructor type does not exist at runtime. Change `import type { EmailService } from './email.service'` to `import { EmailService } from './email.service'`. With `verbatimModuleSyntax` or `consistent-type-imports` lint rules, mark only real types as type imports.
2. **An interface or type alias as the argument.** Interfaces vanish at compile time, so Nest has nothing to use as a token. Inject a class instead, or give the dependency a token and use `@Inject()`:

   ```ts
   export interface Mailer { send(to: string): Promise<void> }
   export const MAILER = Symbol('MAILER')

   // module: providers: [{ provide: MAILER, useClass: SmtpMailer }]
   constructor(@Inject(MAILER) private readonly mailer: Mailer) {}
   ```

3. **Two files that import each other.** If `a.ts` imports `b.ts` and `b.ts` imports `a.ts`, one of them sees `undefined` while loading. A typical case is a module file that exports a token constant, imported by a provider that the same module file imports. Move the constant to its own file. Barrel files (`index.ts`) are another common hidden cause, so import the class from its own file inside the same folder.

## How do I fix a circular dependency?

Remove the cycle if you can, and use `forwardRef()` on both sides if you cannot. A cycle means provider A needs B and B needs A, or module A imports module B and module B imports module A. The simplest fix is to move the shared logic into a third provider that both depend on.

When the cycle is real, wrap both references:

```ts
import { Inject, Injectable, forwardRef } from '@nestjs/common'

@Injectable()
export class CatsService {
  constructor(
    @Inject(forwardRef(() => CommonService))
    private readonly commonService: CommonService,
  ) {}
}

@Injectable()
export class CommonService {
  constructor(
    @Inject(forwardRef(() => CatsService))
    private readonly catsService: CatsService,
  ) {}
}
```

For modules, use `forwardRef(() => OtherModule)` inside `imports` on both modules. Both sides need it: with only one, Nest cannot finish building the metadata.

One NestJS 12 detail: its packages are ES modules, and in an ESM build a plain module cycle fails before Nest starts. Two modules that import each other without `forwardRef` throw this from Node itself:

```text
ReferenceError: Cannot access 'AModule' before initialization
```

Putting `forwardRef(() => AModule)` in `BModule` and `forwardRef(() => BModule)` in `AModule` resolves it. In a CommonJS setup the same mistake shows up as Nest's own `The module at index [0] of the "imports" array is undefined` message instead. The [circular dependency docs](https://docs.nestjs.com/fundamentals/circular-dependency) also describe a `ModuleRef` alternative and warn against barrel files for modules and providers.

## How do I fix this error in a NestJS unit test?

Add the missing provider to the testing module, or replace it with a mock. `Test.createTestingModule` builds a module that contains only what you list, so a service with a dependency fails with `RootTestModule` in the message:

```text
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument EmailService at index [0] is available in the RootTestModule module.
```

Provide a stand-in for the dependency:

```ts
import { Test } from '@nestjs/testing'

const moduleRef = await Test.createTestingModule({
  providers: [
    UsersService,
    { provide: EmailService, useValue: { send: () => 'mock' } },
  ],
}).compile()

const users = moduleRef.get(UsersService)
```

This is the pattern from the [Nest testing docs](https://docs.nestjs.com/fundamentals/testing). If the dependency has its own dependencies, mocking it with `useValue` avoids pulling the whole chain into the test. For choosing a runner, see [Vitest vs Jest vs node:test](https://fabwaseem.com/blog/vitest-vs-jest-vs-node-test).

## Why is my injected service undefined with no error at all?

Because Nest reads constructor types from decorator metadata, and some tools do not emit it. When metadata is missing, the argument is simply `undefined` and Nest raises no resolve error. The first sign is a later crash such as:

```text
TypeError: Cannot read properties of undefined (reading 'send')
```

Two situations produce it:

- **A class without a decorator.** A provider class that has no `@Injectable()` (or other decorator) gets no parameter metadata, so its constructor arguments are not injected.
- **A runner that does not emit decorator metadata.** A Nest app run through `tsx` starts without errors, yet constructor-injected services are `undefined`, because esbuild does not emit decorator metadata. Compile with `tsc` or the Nest CLI, or use a build tool that supports `emitDecoratorMetadata`, and keep `experimentalDecorators` and `emitDecoratorMetadata` enabled in `tsconfig.json`. The official `typescript-starter` tsconfig enables both.

If you only need an injected value to exist, `@Inject(EmailService)` on the parameter gives Nest the token explicitly and works without emitted metadata.

Two more notes for edge cases. A provider with request scope makes every class that injects it request-scoped too, so a singleton that suddenly behaves per request may be inheriting scope; see the [injection scopes docs](https://docs.nestjs.com/fundamentals/injection-scopes). And if a dependency is genuinely optional, mark that parameter with `@Optional()` so Nest injects `undefined` instead of failing.

## How do I debug it step by step?

Work through the message from left to right, then confirm with Nest's own logging:

1. Copy the three names: failing provider, argument and module.
2. Open the named module. Is the provider in `providers`, or is its module in `imports`?
3. Open the module that should supply it. Is the provider in `exports`?
4. If no token name is shown, check for `import type`, interfaces and circular file imports.
5. If the token is `ModuleRef`, check for duplicate `@nestjs/core` copies.
6. Set the `NEST_DEBUG` environment variable to any non-empty string and start the app again. Nest then logs the host class being injected, the dependency name or token, and the module it searches, so you can trace where resolution stops.
7. In a test, add the provider or a mock to `createTestingModule`.

For larger apps, [Nest Devtools](https://docs.nestjs.com/devtools/overview) can help you resolve the error by showing the module graph. The official [common errors page](https://docs.nestjs.com/faq/common-errors) lists the same causes. If you are building a Nest API from scratch, my [secure NestJS and Prisma starter](https://fabwaseem.com/blog/secure-nestjs-prisma-api-starter) shows a module layout where each feature exports only its public service, and the [NestJS rate limiting guide](https://fabwaseem.com/blog/nestjs-rate-limiting-redis-vs-postgres) wires a guard through the same module rules.

## Key takeaways

- The error means a constructor argument has no provider in the declaring module's context. Read the provider, argument and module names in the message.
- A named token usually means the provider is not in `exports` or its module is not in `imports`. Fix it with one line in the module.
- "Appears to be undefined at runtime" means `import type`, an interface used as a token, or a circular file import.
- Circular dependencies need `forwardRef` on both sides; in an ES module build a plain cycle throws a `ReferenceError` first.
- A missing `ModuleRef` points to two copies of `@nestjs/core` in a monorepo.
- In tests, provide the dependency or mock it with `{ provide, useValue }`.
- If injected services are silently `undefined`, the compiler or runner is not emitting decorator metadata.
- Set `NEST_DEBUG` to see which module Nest searched for each dependency.

## Questions

### What does "Nest can't resolve dependencies of the X (?)" mean?

Nest tried to build provider X and found no provider for the constructor argument at the reported index in the module that declares X. The (?) stands for the unresolved argument. Open that module and check that the dependency is provided there or exported by a module it imports.

### Why do I get this error even though I imported the module?

Importing a module only gives you what it exports. If the module lists the service in providers but not in exports, other modules cannot inject it. Add the service to the exports array of the module that provides it.

### What does "appears to be undefined at runtime" mean in NestJS?

The class used as the constructor argument type was undefined when Nest read it. Typical causes are import type on an injectable class, an interface or type used as the token, or two files that import each other. Use a normal import, inject a class or an @Inject token, and break the cycle.

### How do I fix a circular dependency in NestJS?

First try to remove the cycle by moving shared logic into a third provider. If you cannot, use forwardRef(() => OtherClass) inside @Inject on both providers, and forwardRef in the imports array on both modules.

### How do I fix this error in a NestJS unit test?

The testing module only contains what you list. Add the missing provider to Test.createTestingModule, or replace it with a mock using { provide: EmailService, useValue: { send: () => 'mock' } }.

### Why is my injected service undefined without any error?

The class parameter type was not emitted. Nest reads constructor types from decorator metadata, which needs emitDecoratorMetadata with a compiler that supports it. A runner that skips it, such as tsx, leaves the argument undefined instead of raising the resolve error.
