Home > Software design >  Nest cannot create the AuthModule instance
Nest cannot create the AuthModule instance

Time:12-07

when trying to establish links between the user model and the group, an error occurs:

I can't understand why..

[Nest] 30312  - 06.12.2021, 20:53:42     LOG [NestFactory] Starting Nest application...
[Nest] 30312  - 06.12.2021, 20:53:42   ERROR [ExceptionHandler] Nest cannot create the AuthModule instance.
The module at index [1] of the AuthModule "imports" array is undefined.

Potential causes:
- A circular dependency between modules. Use forwardRef() to avoid it. Read more: https://docs.nestjs.com/fundamentals/circular-dependency
- The module at index [1] is of type "undefined". Check your import statements and the type of the module.

Scope [AppModule]
Error: Nest cannot create the AuthModule instance.
The module at index [1] of the AuthModule "imports" array is undefined.

app.module.ts https://pastebin.com/T9EC05gS

users.module.ts https://pastebin.com/inS044Xk

user.model.ts https://pastebin.com/wpyUs2SW

groups.module.ts https://pastebin.com/Jvr2Fuk3

group.model.ts https://pastebin.com/nBvkaEUt

auth.module.ts https://pastebin.com/MYQ2dw6y

auth.guard.ts https://pastebin.com/xDpQ8vwE

auth.service.ts https://pastebin.com/G9TzN3XF

Thanks in advance

UPD: error appears when adding

  @Expose()
  @HasMany(() => Group)
  groups: Group[];

to user.model.ts

CodePudding user response:

in your auth.module.ts file, instead of
import { UsersModule } from '../users'; try
import { UsersModule } from '../users/user.module';

I believe that these barrel files (index.ts exporting everything) isn't a good pattern to follow here as it introduces circular imports between NestJS modules.


1 auth.module.ts imports users/index.ts (which exports user.module.ts I guess)
2 groups.module.ts imports auth/index.ts (which exports auth.module.ts I guess)
3 user.model.ts imports groups/index.ts

then you'll end up with 1 importing 3 and 3 importing 1. That's the problem.

  • Related