Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected circular dependency error #1614

Closed
LandazuriPaul opened this issue Mar 6, 2019 · 4 comments
Closed

Unexpected circular dependency error #1614

LandazuriPaul opened this issue Mar 6, 2019 · 4 comments

Comments

@LandazuriPaul
Copy link

LandazuriPaul commented Mar 6, 2019

I'm submitting a...


[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request

Current behavior

Nest throws an error about a circular dependency that doesn't exist. Here is the error thrown:
Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it. (Read more https://docs.nestjs.com/fundamentals/circular-dependency.) Scope [APIModule -> AuthModule]

Expected behavior

From what I understand about circular dependency, there isn't any in the situation described, so Nest should be able to create the module instance.

Minimal reproduction of the problem with instructions

api.module.ts

import { Module } from '@nestjs/common';

import { AuthModule, UserModule } from '@modules';

@Module({
  imports: [
    AuthModule,
    UserModule,
  ],
})
export class APIModule {}

auth.module.ts

import { Module } from '@nestjs/common';

import { AuthService } from '@api/services';
import { UserModule } from '@modules';
import { UserService } from '@domain/services';

@Module({
  imports: [
    UserModule
  ],
  providers: [
    AuthService,
    UserService,
  ],
  exports: [AuthService]
})
export class AuthModule {}

user.module.ts

import { Module } from '@nestjs/common';

import { UserService } from '@domain/services';

@Module({
  providers: [UserService],
  exports: [UserService],
})
export class UserModule {}

And for more information, here are the 2 services:

auth.service.ts

import { Injectable, Logger } from '@nestjs/common';

import { UserService } from '@domain/services';

@Injectable()
export class AuthService {
  private logger = new Logger(AuthService.name);

  constructor(
    private readonly usersService: UserService,
  ) {}
}

user.service.ts

import {
  Injectable,
  Logger,
} from '@nestjs/common';

@Injectable()
export class UserService {
  private logger = new Logger(UserService.name);
}

Environment


Nest version: 5.4.0
Node version: 11.0.0
@LandazuriPaul
Copy link
Author

LandazuriPaul commented Mar 7, 2019

Trying to solve my issue, I'm discovered that if I import the modules directly (instead of using the modules/index.ts with intermediate exports), Nest doesn't complain at all and everything works fine.

So as a fix, I've found a solution. I just have to replace my module imports with this:

api.module.ts

import { AuthModule } from '@modules/auth/auth.module';
import { UserModule } from '@modules/user/user.module';

...

auth.module.ts

import { UserModule } from '@modules/user/user.module';

...

But still, I'm curious to understand why there is an issue with my first approach. I guess I've misunderstood something about Nest dependencies or maybe it's not Nest related at all and just the way imports work in Node.js / TypeScript environment. And if so, I'm curious to know if there is a way to gather all declarations living in a directory (here, modules) in an index.ts. Because I feel that my initial approach was more developper-friendly, as I could import all my modules from a single point.

So if anyone has any input or explanation about this behaviour, it would be very welcomed! :)

And besides, the Nest framework and the way it is lead are very promising and interesting!

Dziękuję @kamilmysliwiec !

@kamilmysliwiec
Copy link
Member

Dziękuje @LandazuriPaul! :)

I'm glad that you found a solution. Here you can read more about this issue: #1181

@LandazuriPaul
Copy link
Author

Thanks for the issue reference!

I'll read about barrel files and its implications. I didn't know about all the outcomes tied to them.

@lock
Copy link

lock bot commented Sep 24, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants