0

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.
renanz
  • 11
  • 2
  • 3

1 Answers1

0

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

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
  • There's no circular reference. If there where so, Nest tells you when you have a circular dependency. – renanz Apr 22 '21 at 22:03
  • I didn't say it was a circular reference (i.e. classA depends on classB depends on classA) I said it was a circular file import (fileA imports fileB imports fileA) and this chain can be rather well nested, but it is a circular file import. If you can provide access to your code, I can find it. – Jay McDoniel Apr 22 '21 at 22:07
  • I moved the firebase module to its own folder and it fixed it! Thanks – renanz Apr 22 '21 at 22:16
  • I'm guessing it was a barrel file import? – Jay McDoniel Apr 22 '21 at 22:19
  • Yes, I think it had to do with that I was exporting the whole folder in an index file and in another file I was using that service. – renanz Apr 23 '21 at 00:57