1

I have my app.module that imports UserModule and AuthModule.

@Module({
  imports: [UserModule, AuthModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

This is how my AuthModule looks:

@Module({
  imports: [forwardRef(() => UserModule)],
  controllers: [AuthController],
  providers: [AuthService, UserService],
  exports: [AuthModule],
})
export class AuthModule {}

And UserModule:

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

I inject userService in AuthService. If i delete AuthModule from AppModule, the dependency disapears, so, the problem is maybe somewhere there.

3
  • 1
    If you're injecting UserService in AuthService why did you not export it? exports: [UserService] (in UserModule) Commented Feb 1, 2021 at 16:30
  • @MicaelLevi, yeah, I solved it by trying random combination of imports and exports. Thank you, now i know how and why it works. Commented Feb 1, 2021 at 16:53
  • @Helios could you please provide your final solution that made it works? Commented Jun 29, 2022 at 10:04

3 Answers 3

3

If you think there shouldn't be any circular dependency issue because you have follow the concept of SOLID... then you are right.. there should not be any circular dependency issue UNLESS your imports are wrong..

typescript gives facility of absolute path but always use relative path while importing

Wrong Way

import {Category} from '/src/categories';

Right Way

import {Category} from '../categories';

do this in your entire project your circular dependency will be resolved

Sign up to request clarification or add additional context in comments.

Comments

2

First of all, configure the user module and auth module so that they can refer to each other.

@Module({
  imports: [
    forwardRef(() => UserModule),
  ],
  exports: [AuthService],
  controllers: [AuthController],
  providers: [AuthService],
})
export class AuthModule {}

@Module({
  imports: [
    forwardRef(() => AuthModule),
  ],
  exports: [UserService],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

In this situation, two different services that perform dependency injection must also be configured so that cross-references are possible.

@Injectable()
export class UserService {
  constructor(
    @Inject(forwardRef(() => AuthService))
  )
}

@Injectable()
export class AuthService {
  constructor(
    @Inject(forwardRef(() => UserService))
  )
}

Please try and comment if it doesn't work.

1 Comment

I already solved my problem, but i tried this approach and it did not work. The problem is maybe that these modules are also imported in AppModule.
0

If you strongly believe that circular dependency is a false positive, check that if you happen to have a JSON file with the same name as a .ts file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.