BlogEx
BACK TO FEED
2024.01.08 ● PUBLIC

NestJS cannot resolve dependency that's clearly there

debugnestjstypescript

SYMPTOMS

When trying to start the app:

[Nest] 71998 - 11/12/2023, 5:12:05 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the OfacListReloaderService (LOGGER_SERVICE, ?, OfacListDownloaderService). Please make sure that the argument dependency at index [1] is available in the ScreeningModule context.
Potential solutions:
- Is ScreeningModule a valid NestJS module?
- If dependency is a provider, is it part of the current ScreeningModule?
- If dependency is exported from a separate @Module, is that module imported within ScreeningModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})
Error: Nest can't resolve dependencies of the OfacListReloaderService (LOGGER_SERVICE, ?, OfacListDownloaderService). Please make sure that the argument dependency at index [1] is available in the ScreeningModule context.
Potential solutions:
- Is ScreeningModule a valid NestJS module?
- If dependency is a provider, is it part of the current ScreeningModule?
- If dependency is exported from a separate @Module, is that module imported within ScreeningModule?
@Module({
imports: [ /* the Module containing dependency */ ]
})

Screening module:

import { Module } from "@nestjs/common";
import { LoggingModule } from "@org/logging";
import { AppConfigModule } from "@org/config";
import { StorageModule } from "@org/storage";
import { DataModule } from "@org/data";
import { SanctionsScreeningService } from "./sanctions-screening.service";
import { OfacListDownloaderService, OfacListReloaderService } from "./ofac";
import { CsvListDownloaderService } from "./csv-list-downloader.service";
@Module({
imports: [
LoggingModule.forRoot(),
AppConfigModule.forRoot(),
StorageModule.forRoot(),
DataModule.forRoot(),
],
providers: [
SanctionsScreeningService,
OfacListDownloaderService,
OfacListReloaderService,
CsvListDownloaderService,
],
exports: [SanctionsScreeningService],
})
export class ScreeningModule {}

Service in question:

@Injectable()
export class OfacListReloaderService implements OnModuleInit {
constructor(
@Inject(LOGGER_SERVICE) private readonly logger: ILogger,
@Inject(SanctionsScreeningService)
private readonly screening: SanctionsScreeningService,
@Inject(OfacListDownloaderService)
private readonly downloader: OfacListDownloaderService
) {
this.logger.setContext(OfacListReloaderService.name);
}
}

The SanctionsScreeningService is clearly on the providers list of ScreeningModule.

FIX

In that case it was due to circular imports between some models, eh.