Search code examples
nestjstypeorm

Nest can't resolve dependencies although module is exported and imported Nestjs


I have 2 modules

@Module({
  imports: [
    TypeOrmModule.forFeature([User]),
    forwardRef(() => FriendModule),
    forwardRef(() => RequestFriendModule),
  ],
  providers: [UserService],
  controllers: [UserController],
  exports: [UserService],
})
export class UserModule {}

@Module({
  controllers: [NotificationController],
  providers: [NotificationService],
  imports: [UserModule],
  exports: [NotificationService],
})
export class NotificationModule {}

In my notification.service:

 constructor(
    private readonly userService: UserService,
 )

And i got error: Nest can't resolve dependencies of the NotificationService (?). Please make sure that the argument dependency at index [0] is available in the NotificationModule context

I exported UserService in UserModule, then import UserModule in NotificationModule.

Why this happen? And how can I resolve it?


Solution

  • as you can see in the docs here, since you have 'dependency at index ..', that's because you got a circular dependency.

    You can learn more about them here: Circular Dependencies in NestJS and how to Avoid Them