Nest js - circular reference at the moment to inject a repository

Viewed 7974

Every time I try to run my project, I get the error:

CircularDependencyException [Error]: A circular dependency has been detected inside @InjectRepository(). Please, make sure that each side of a bidirectional relationships are decorated with "forwardRef()". Also, try to eliminate barrel files because they can lead to an unexpected behavior too.

The only clue I have is one of my modules:

If I comment this line:

constructor(
        @InjectRepository(Role) private roleRepo: Repository<Role>
    ){}

and the project runs, but I notice the log when I start the project, the Module with the error starts before everything

[Nest] 16872   - 08/04/2020, 7:56:24 PM   [NestFactory] Starting Nest application...
[Nest] 16872   - 08/04/2020, 7:56:24 PM   [InstanceLoader] TypeOrmModule dependencies initialized +91ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] MyErrorModule dependencies initialized +500ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +630ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +2ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +1ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +4ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] AuthorizationModule dependencies initialized +3ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] AppModule dependencies initialized +3ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] FirstModule dependencies initialized +2ms

But I have my app.module.ts file with

@Module({
  imports: [TypeOrmModule.forRootAsync({
    useClass: DatabaseConnectionService
  }), 
  AuthorizationModule,
  TypeOrmModule.forFeature([User, Role]),
  FirstModule, 
  SecondModule,
  MyErrorModule, //This is the first to be executed
],

Someone has an idea about how to solve this issue?

3 Answers

Well, I decided to not delete this question, because it could be helpful for others.

This part of the log was important to debug my issue:

[Nest] 16872   - 08/04/2020, 7:56:24 PM   [NestFactory] Starting Nest application...
[Nest] 16872   - 08/04/2020, 7:56:24 PM   [InstanceLoader] TypeOrmModule dependencies initialized +91ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] MyErrorModule dependencies initialized +500ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +630ms
[Nest] 16872   - 08/04/2020, 7:56:25 PM   [InstanceLoader] TypeOrmModule dependencies initialized +2ms

And I notice one of the modules was registered before everything and the reason was: I had an unused import in one of my services and that import contained another service and that was producing the circular reference.

That's why in this thread: https://github.com/nestjs/nest/issues/3555#issuecomment-562468943 written by Kamil Mysliwiec

This error means that you've passed undefined value into @InjectRepository() decorator. We can't really produce anything more descriptive :( Ensure you don't have any circular dependencies between your entity<->service.

And it's true, with the import I tried to inject an unregistered Module/Service that wasn't ready yet. So, the solution FOR THIS SPECIFIC CASE, clean the code, and remove the unused imports.

in my case it was because of wrong "paths". I used shorthands for paths using "@" and IDE told me that the path is ok but nest.js said that it was a circular dependecy.

I faced the same issue. For me, it was resolved by reordering imports.

Related