#Issue with circular dependency

4 messages · Page 1 of 1 (latest)

quaint sluice
#

These are my modules:

GlobalModule

import { Module } from '@nestjs/common';
import { GlobalService } from './global.service';
import { AsnModule } from '@/modules/asn/asn.module';
import { ConfigModule } from '@/modules/config/config.module';

@Module({
  imports: [ConfigModule, AsnModule],
  providers: [GlobalService],
  exports: [GlobalService]
})
export class GlobalModule {}

AsnModule

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

import { TasksModule } from '@/modules/task/tasks.module';
import { PeopleModule } from '@/modules/people/people.module';
import { ConfigModule } from '@/modules/config/config.module';
import { GremlinModule } from '@/modules/gremlin/gremlin.module';

import { AsnService } from './asn.service';
import { AsnController } from './asn.controller';
import { AsnRepository } from './asn.repository';
import { FuzzySearchModule } from '../fuzzy-search/fuzzy-search.module';

@Module({
  imports: [
    ConfigModule,
    PeopleModule,
    GremlinModule,
    TasksModule,
    FuzzySearchModule
  ],
  controllers: [AsnController],
  providers: [AsnService, AsnRepository],
  exports: [AsnService, AsnRepository]
})
export class AsnModule {}

FuzzySearchModule

import { Module } from '@nestjs/common';
import { AsnSearchService } from './asn-search.service';
import { GlobalModule } from '@/modules/global/global.module';

@Module({
  imports: [GlobalModule],
  providers: [AsnSearchService],
  exports: [AsnSearchService]
})
export class FuzzySearchModule {}
#

I was getting this error

Error: Nest cannot create the GlobalModule instance.
The module at index [1] of the GlobalModule "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 -> AsnModule -> FuzzySearchModule]

So I changed my GlobalModule to this

import { Module, forwardRef } from '@nestjs/common';
import { GlobalService } from './global.service';
import { AsnModule } from '@/modules/asn/asn.module';
import { ConfigModule } from '@/modules/config/config.module';

@Module({
  imports: [ConfigModule, forwardRef(() => AsnModule)],
  providers: [GlobalService],
  exports: [GlobalService]
})
export class GlobalModule {}

But now I am facing this another issue

Potential solutions:
- Is GlobalModule a valid NestJS module?
- If dependency is a provider, is it part of the current GlobalModule?
- If dependency is exported from a separate @Module, is that module imported within GlobalModule?
   @Module({
     imports: [ /* the Module containing dependency */ ]
   })

I tried fixing it by adding @Inject(forwardRef(()=>AsnService)) . But after this there are some modules that are not mapping. How can I fix this? Can anyone assist me? Thanks!

steep jasper
#

If possible, try to extract whatever FuzzySearch module needs from global module to a new module.

jolly musk
#

But after this there are some modules that are not mapping.
Can you give more detial around this? What wodCZ said is the better solution, but that can sometimes take time to re-implement, so I can try to help find a quick fix if there's more info provided