Nest can't resolve dependencies of the Service in Global Module

2.9k views Asked by At

I have a global module called FirebaseModule which exports a FirebaseService, I import this module to the app.module.ts and then I try to use the FirebaseService inside a UsersService and it fails with a missing dependency issue.

Something interesting is that if I remove the usage on the service and use it only on the controller nothings fails but if I want to use it in the service provider, it starts failing.

My code:

app.module.ts

@Module({
  imports: [
    CustomLoggerModule,
    ConfigurationModule,
    TypeOrmModule.forRootAsync({
      useExisting: ConfigurationService,
    }),
    FirebaseModule,
    UsersModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

firebase.module.ts

@Global()
@Module({
  imports: [],
  providers: [FirebaseService],
  exports: [FirebaseService],
})
export class FirebaseModule {}

users.module.ts

@Module({
  imports: [CqrsModule, TypeOrmModule.forFeature([UsersRepository])],
  controllers: [UsersController],
  providers: [UsersService, ...CommandHandlers, ...EventHandlers],
  exports: [UsersService, TypeOrmModule.forFeature([UsersRepository])],
})
export class UsersModule {}

users.controller.ts

@Controller('users')
@UseGuards(AuthGuard('firebase'))
export class UsersController {
  constructor(
    private readonly usersService: UsersService,
    private readonly firebaseService: FirebaseService,
  ) {}
}

users.service.ts

@Injectable()
export class UsersService {
  constructor(
    private readonly firebaseService: FirebaseService,
    private readonly repository: UsersRepository,
  ) {}
}

I get the following error:

Nest can't resolve dependencies of the UsersService (?, UsersRepository). Please make sure that the argument dependency at index [0] is available in the UsersModule context.
1

There are 1 answers

5
Jay McDoniel On

You have a circular file reference in your UserService to the FirebaseService most likely. If Nest is able to resolve the name of the class/provider/injection token, it will say what that token is explicitly, but when it is unable to, all the comes out is dependency. Here's a little more information from the docs