I have module A and B that depend on each other. Module C depends on Module B.
I used forwardRef()
for modules A and B, but I didn't for C and B,
// a.module.ts
@Module(
{
imports : [
forwardRef(() => ModuleB),
forwardRef(() => ModuleC),
],
providers: [
ServiceA,
],
exports : [
ServiceA,
]
},
)
export class ModuleA {}
// a.service.ts
constructor(
@Inject(forwardRef(() => ServiceB)) private readonly serviceB: ServiceB,
@Inject(forwardRef(() => ServiceC)) private readonly serviceC: ServiceC,
) {}
// b.module.ts
@Module(
{
imports : [
forwardRef(() => ModuleA ),
ModuleC,
],
providers: [
ServiceB,
],
exports : [
ServiceB,
]
},
)
export class ModuleB {}
// b.service.ts
constructor(
@Inject(forwardRef(() => ServiceA)) private readonly serviceA: ServiceA,
private readonly serviceC: ServiceC,
) {}
// c.module.ts
@Module(
{
imports : [
forwardRef(() => ModuleA),
],
providers: [
ServiceB,
],
exports : [
ServiceB,
]
},
)
export class ModuleC {}
// c.service.ts
constructor(
@Inject(forwardRef(() => ServiceA)) private readonly serviceA: ServiceA,
) {}
It causes a circular dependency between the modules issue, but I can't track the root as module A is in another module, and the modules are deeply integrated/nested. I don't know how circular dependencies are resolved in NestJS, or how to debug better. I read that I should avoid it as much as possible.