'"Cannot find module" with NestJS

So, I have my RootModule, a ProductModule and a StoreModule.

My RootModule:

@Module({
  imports: [
    ProductModule,
    StoreModule,
  ],
  controllers: [],
  providers: [],
})
export class RootModule {}

My ProductModule:

@Module({
  providers: [ProductService],
  exports: [],
})
export class ProductModule {}

My StoreModule:

@Module({
  imports: []
  providers: [StoreService, StoreWaitingService],
})
export class ProductModule {}
  1. Since my RootModule imports ProductModule, shouldn't I be able to use ProductService on StoreService and StoreWaitingService?

  2. What if I imported ProductModule on StoreModule imports?



Solution 1:[1]

You can use Product Module and Service in StoreService

Product Module

@Module({
  providers: [ProductService],
  exports: [ProductService],
})
export class ProductModule {}

Store Module

@Module({
  imports: [ProductModule],
})
export class StoreModule{}

Store service

import ProductService from '<Product Service Path>';
...

constructor(private readonly productService: ProductService){}

...
//in Function
this.productService.someFunction();

But if you don't want to import the product module in Store Module, you can make the Product Module a global Module - https://docs.nestjs.com/modules#global-modules

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nicholi Jin