I have this folder structure:
.
├── libs/
│ └── dma/
│ ├── client/
│ │ ├── client.module.ts
│ │ └── client.service.ts
│ ├── cmts/
│ │ ├── cmts.module.ts
│ │ └── cmts.service.ts
│ └── dma.module.ts
└── src/
├── app.module.ts
└── app.service.ts
And I have this module structure:
And here is my source code:
~/src/app.service.ts
@Injectable()
export class AppService {
constructor(private readonly cmtsService: CmtsService) {}
}
~/src/app.module.ts
@Module({
imports: [
DmaModule.forRootAsync({
useFactory: () => ({
dmaHost: 'DMA_HOST',
superadminUsername: 'DMA_SUPERADMIN_USERNAME',
superadminPassword: 'DMA_SUPERADMIN_PASSWORD',
}),
})],
providers: [AppService]
})
class AppModule{}
~/libs/dma/dma.module.ts
export interface DmaModuleProps {
dmaHost: string
superadminUsername: string
superadminPassword: string
}
export interface RootAsyncOptions {
useFactory: (...args: any[]) => DmaModuleProps
}
@Module({
imports: [CmtsModule],
providers: [],
exports: [CmtsModule],
})
export class DmaModule{
static forRootAsync(options: RootAsyncOptions): DynamicModule {
const DmaOptionsFactoryProvider: FactoryProvider = {
provide: 'DMA_OPTIONS',
useFactory: options.useFactory,
}
return {
module: DmaModule,
providers: [DmaOptionsFactoryProvider],
exports: [DmaOptionsFactoryProvider],
}
}
}
~/libs/dma/client/client.module.ts
@Module({
imports: [DmaModule],
providers: [ClientService],
exports: [ClientService],
})
export class ClientModule{}
~/libs/dma/client/client.service.ts
@Injectable()
export class ClientService {
constructor(
@Inject('DMA_OPTIONS') private readonly dmaProps: DmaModuleProps,
) {}
}
~/libs/dma/cmts/cmts.module.ts
@Module({
imports: [ClientModule],
providers: [CmtsService],
exports: [CmtsService]
})
export class CmtsModule {}
~/libs/dma/cmts/cmts.service.ts
@Injectable()
export class CmtsService {
constructor(private readonly clientService: ClientService) {}
}
I am getting this error:
Nest] 642956 - 07/15/2024, 9:47:12 PM ERROR [ExceptionHandler] Nest cannot create the ClientModule instance.
The module at index [0] of the ClientModule "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 [0] is of type "undefined". Check your import statements and the type of the module.
Scope [AppModule -> DmaModule -> CmtsModule]
2