typescriptnestjscircular-dependency

Nestjs circular dependency using forwardref


Hi guys i am getting circular dependency error on nestjs i tried using the forwardref but that doesn't help 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 {}

In the Service i am accessing the other 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:

Why i got circular dependency !!


Solution

  • The solution was to use the @Inject() in the constructor (where did i import the services) you can see more in the docs