nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.9k stars 7.55k forks source link

Circular dependencies #13001

Closed Sara2706 closed 8 months ago

Sara2706 commented 8 months ago

Is there an existing issue for this?

Current behavior

getting Circular dependencies error when i start my server

this is my code GatewayModule @Module({ providers: [SocketGateway], imports: [ Notification, PassportModule.register({ defaultStrategy: 'dashboard' }), MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), MongooseModule.forFeature([ { name: UserAuth.name, schema: UserAuthSchema }, ]), NotificationModule, forwardRef(() => AiServicesModule), MessagingModule, ChannelModule, StorageModule ], exports: [SocketGateway, StorageModule], }) export class GatewayModule { }

AiServicesModule @Module({ imports: [ UserFilesModule, MongooseModule.forFeature([{ name: UserFiles.name, schema: UserFilesSchema }]), MongooseModule.forFeature([{ name: Channel.name, schema: ChannelSchema }]), MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), forwardRef(() => GatewayModule), StorageModule, MessagingModule ], controllers: [AiServicesController], providers: [AiServicesService], exports: [AiServicesService], }) export class AiServicesModule { }

and services like AiServicesService

export class AiServicesService { constructor( @InjectModel(UserFiles.name) private userFileModel: Model, @InjectModel(Channel.name) private channelModel: Model, @InjectModel(User.name) private userModel: Model, private userFilesService: UserFilesService, private storageService: StorageService, private messagingService: MessagingService, @Inject(forwardRef(() => SocketGateway)) private socketGateway: SocketGateway, ) { }

gateway export class SocketGateway implements OnModuleInit { constructor( private storageService: StorageService, private notificationservice: NotificationService, private channelService: ChannelService, @Inject(forwardRef(() => AiServicesService)) private aiservicesservice: AiServicesService, private messagingService: MessagingService, @InjectModel(User.name) private userModel: Model, @InjectModel(UserAuth.name) private userAuthModel: Model, ) { }

i already tried forwardRef in module and @Inject in service but stilll im facing error like
[Nest] 340 - 03/01/2024, 3:00:25 pm ERROR [ExceptionHandler] Nest cannot create the GatewayModule instance. The module at index [7] of the GatewayModule "imports" array is undefined.

Potential causes:

Scope [AppModule -> ProjectModule -> ChannelModule -> SlackModule -> AiServicesModule] Error: Nest cannot create the GatewayModule instance.

Minimum reproduction code

https://github.com

Steps to reproduce

  1. npm i
  2. npm run start:dev

Expected behavior

server need to start

Package

Other package

No response

NestJS version

^10.0.0

Packages versions

"dependencies": { "@google-cloud/storage": "^7.1.0", "@liaoliaots/nestjs-redis": "^9.0.5", "@nestjs/cache-manager": "^2.1.1", "@nestjs/common": "^10.0.0", "@nestjs/config": "^3.0.0", "@nestjs/core": "^10.0.0", "@nestjs/jwt": "^10.1.0", "@nestjs/mongoose": "^10.0.1", "@nestjs/passport": "^10.0.1", "@nestjs/platform-express": "^10.0.0", "@nestjs/platform-socket.io": "^10.2.5", "@nestjs/swagger": "^7.1.10", "@nestjs/websockets": "^10.2.5", "@sendgrid/mail": "^7.7.0", "@slack/bolt": "^3.14.0", "@slack/events-api": "^3.0.1", "@slack/rtm-api": "^6.1.1", "@slack/web-api": "^6.9.0", "bcryptjs": "^2.4.3", "cache-manager": "^5.2.4", "cache-manager-redis-store": "^3.0.1", "cheerio": "^1.0.0-rc.12", "class-validator": "^0.14.0", "compression": "^1.7.4", "cookie-parser": "^1.4.6", "dotenv": "^16.3.1", "express": "^4.18.2", "file-saver": "^2.0.5", "ioredis": "^5.3.2", "mongoose": "^7.5.0", "multer": "^1.4.5-lts.1", "nestjs-cache": "^1.0.2", "nestjs-redis": "^1.3.3", "nestjs-slack-listener": "^1.1.3", "openai": "^3.1.0", "passport": "^0.6.0", "passport-jwt": "^4.0.1", "passport-slack": "^0.0.7", "redis": "^4.6.10", "reflect-metadata": "^0.1.13", "rxjs": "^7.8.1", "socket.io": "^4.7.2", "stripe": "^14.4.0", "swagger-ui-express": "^5.0.0", "uuid": "^9.0.1" },

Node.js version

20.10.0

In which operating systems have you tested?

Other

I have a module import error related to the GatewayModule. The error message states: "Nest cannot create the GatewayModule instance. The module at index [7] of the GatewayModule 'imports' array is undefined."

Potential causes:

micalevisk commented 8 months ago

:warning: We use GitHub Issues to track bug reports, feature requests and regressions

Try the following instead: