NestJS "Nest can't resolve dependencies of the X (?)" in CI
Nest built the dependency graph and one constructor argument, shown as (?), has no provider it can inject. The token is either not declared as a provider in the current module or not exported by the module you imported.
What this error means
App bootstrap or a TestingModule compile fails with "Nest can't resolve dependencies of the X (?). Please make sure that the argument dependency at index [n] is available in the Y context." The ? marks the argument Nest could not satisfy.
[Nest] ERROR [ExceptionHandler] Nest can't resolve dependencies of the CatsService (?).
Please make sure that the argument UsersService at index [0] is available in the CatsModule context.
Potential solutions:
- Is CatsModule a valid NestJS module?
- If UsersService is a provider, is it part of the current CatsModule?
- If UsersService is exported from a separate @Module, is that module imported within CatsModule?Common causes
The provider is not registered in the module
The injected class is not listed in the providers array of the module that declares the consumer, so Nest has no token to inject.
The provider lives in another module that does not export it
The dependency is a provider in a different module, but that module's exports array does not include it, so importing the module still hides the token.
How to fix it
Provide or export the token in the right module
- Read the
(?)argument name and the context module in the error. - Add the class to that module's
providers, or export it from its owning module. - Import the owning module wherever the consumer needs it.
@Module({
imports: [UsersModule], // UsersModule must export UsersService
providers: [CatsService],
})
export class CatsModule {}Export the shared provider from its owning module
A provider is private to its module unless listed in exports. Export it so importers can inject it.
@Module({
providers: [UsersService],
exports: [UsersService],
})
export class UsersModule {}How to prevent it
- Keep each provider registered in exactly one module and export it if shared.
- Compile a TestingModule in unit tests so DI gaps fail fast in CI.
- Prefer importing whole modules over re-declaring providers in several places.