You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nest.js循环依赖无法解析求助:forwardRef模块导入仍注入失败

NestJS循环依赖场景下NotificationsService注入失败问题

遇到如下错误:

ERROR [ExceptionHandler] Nest can't resolve dependencies of the InventoryElementsService (PrismaService, ?, AuthorizerService). Please make sure that the argument dependency at index [1] is available in the InventoryElementsModule context.

相关代码

InventoryElementsService构造函数

@Injectable()
export class InventoryElementsService {
    constructor(
        private readonly prisma: PrismaService,
        private readonly notificationsService: NotificationsService,
        private readonly authorizer: AuthorizerService,
    ) { }
...
}

InventoryElementsModule配置

@Module({
    imports: [
        forwardRef(() => UsersModule),
        forwardRef(() => UserPreferenceModule),
        forwardRef(() => NotificationsModule),
        forwardRef(() => InventoryAttributesModule),
        AuthorizerModule
    ],
    providers: [
        InventoryElementsResolver,
        InventoryElementsService,
        PrismaService,
    ],
    exports: [InventoryElementsService]
})
export class InventoryElementsModule {}

NotificationsModule配置

@Module({
    imports: [
        forwardRef(() => UserPreferenceModule),
        forwardRef(() => InventoryElementsModule),
        EmailModule
    ],
    providers: [
        NotificationsResolver,
        NotificationsService,
        PrismaService
    ],
    exports: [NotificationsService]
})
export class NotificationsModule {}

临时解决方案(非最优)

在InventoryElementsService的构造函数中通过@Inject(forwardRef(() => NotificationsService))显式注入可正常工作:

@Injectable()
export class InventoryElementsService {
    constructor(
        private readonly prisma: PrismaService,
        @Inject(forwardRef(() => NotificationsService)) private readonly notificationsService: NotificationsService,
        private readonly authorizer: AuthorizerService,
    ) { }
}

已排查过模块导入和AppModule配置,仍找不到根本问题,寻求帮助。

内容的提问来源于stack exchange,提问作者Felix Moser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 21:01:05