NestJS "UnknownDependenciesException" in CI
UnknownDependenciesException is the underlying error class Nest throws when the injector meets a token it cannot resolve. It names the injectable and the argument index that failed.
What this error means
Bootstrap or a test fails with "UnknownDependenciesException [Error]: Nest can't resolve dependencies of the X". A custom injection token or a class that was never registered as a provider is the usual trigger.
UnknownDependenciesException [Error]: Nest can't resolve dependencies of the
ReportService (?, ConfigService). Please make sure that the argument
"PDF_RENDERER" at index [0] is available in the ReportModule context.Common causes
A custom provider token is not registered
You inject with @Inject('PDF_RENDERER') but no provider uses that exact string or symbol token in the module.
A class is injected without @Injectable or a providers entry
A class used as a dependency is missing the @Injectable() decorator or is not in any module's providers, so Nest has no metadata to construct it.
How to fix it
Register the custom token as a provider
- Match the
@Inject(TOKEN)string or symbol to aprovide:entry exactly. - Add the custom provider to the module that hosts the consumer.
- Re-run so the injector can resolve the token.
@Module({
providers: [
{ provide: 'PDF_RENDERER', useClass: PdfRenderer },
ReportService,
],
})
export class ReportModule {}Add @Injectable and register the class
Decorate the dependency and list it in providers so Nest emits the constructor metadata it needs.
@Injectable()
export class PdfRenderer {}How to prevent it
- Use a shared constant for custom injection tokens so provide and inject cannot drift.
- Decorate every provider with @Injectable().
- Cover module wiring with a compiling TestingModule in CI.