Reputation: 12044
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?
Upvotes: 14
Views: 21336
Reputation: 974
I had an Entity called "Transaction", my IDE automatically imported a class Transaction. But a class from typeorm.
If youre getting this error, look if all the things you use are imported correctly, wasted way too much time on that
Upvotes: 1
Reputation: 21
I fixed this by checking the imports in the respective .module.ts file.
I had renamed an entity but Visual Studio Code didn't pick this particular aspect of the change up, nor did the error log shed any useful light on the root cause of the issue.
Upvotes: 1
Reputation: 1250
The same issue appeared for me after updating Nest to the 10th version. My solution was just changing the order of the imports in the problematic module.
From this:
imports: [
forwardRef(() => UserModule),
PassportModule,
],
to this:
imports: [
PassportModule,
forwardRef(() => UserModule),
],
Upvotes: 0
Reputation: 407
If someone is dealing the same circular dependency error, I found a way to find the problematic code line.
1- First need to install a eslint plugin
yarn add -D eslint-plugin-import
2- Set the configration in eslintrc.js
module.exports = {
"parser": "@typescript-eslint/parser",
"plugins": [
"import",
// ...
],
"extends": [
"plugin:import/typescript",
// ...
],
"rules": {
"import/no-cycle": 2,
// ...
},
// ...
};
After done the above steps, it might need to reload the text editor too. Source
09.07.2023 Edit: Found another way to check
1- Install madge
yarn add -D madge
2- Check circular dependencies with providing tsconfig
yarn madge --ts-config ./tsconfig.json --circular --extensions js,ts src/
Upvotes: 4
Reputation: 10772
In my case this was an incorrect error generated by NestJS. I was just starting the project and had an empty array of providers within the module file. In the compiled NestJS code we see that when no providers exist then a circular dependency error is thrown:
For now I'm just commenting out the providers array altogether:
@Module({
// imports: [ApiAppModule],
controllers: [UserController],
// providers: [],
})
export class UserModule {}
Upvotes: 0
Reputation: 886
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.
Upvotes: 5
Reputation: 41
I faced the same issue. For me, it was resolved by reordering imports.
Upvotes: 2
Reputation: 12044
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.
Upvotes: 13