Hey everyone, I've been running into a circular dependency issue with NestJS. I tried using the forwardref
method, but it hasn't resolved the problem for me.
// AuthModule
@Module({
imports: [
forwardRef(() => UserModule),
JwtModule.register({
global: true,
secret: process.env.TOKEN_SECRET,
signOptions: { expiresIn: process.env.TOKEN_DURATION },
}),
],
providers: [AuthService, AuthGuard],
controllers: [AuthController],
exports: [AuthService],
})
export class AuthModule {}
// UserModule
@Module({
imports: [forwardRef(() => AuthModule)],
controllers: [UserController],
providers: [UserService],
exports: [UserService],
})
export class UserModule {}
Within the service, I am accessing another service like this:
private readonly authService: AuthService,
== CMD ERROR == [Nest] 2788 - 06/10/2023, 12:56:50 PM LOG [InjectorLogger] Nest encountered an undefined dependency. This may be due to a circular import or a missing dependency declaration. [Nest] 2788 - 06/10/2023, 12:56:50 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the UserService (PrismaService, ?, CONFIGURATION(app)). Please make sure that the argument dependency at index [1] is available in the UserModule context.
Potential solutions:
Is UserModule considered a valid NestJS module?
If the dependency is a provider, is it included in the current UserModule?
If the dependency is exported from a separate @Module, ensure that module is imported within UserModule.
@Module({ imports: [ /* the Module containing dependency */ ] })
How did I end up with a circular dependency situation?