Sikau

NestJS · Day 4 · about 30 min

Feature modules

imports, exports and boundaries.

One module per feature

As the API grows, group each feature — dishes, orders, users — into its own module. A module's providers are private unless it exports them; another module must import it to use them. Those boundaries keep big apps understandable.

A module that shares its service

import { Module } from "@nestjs/common";
import { DishesController } from "./dishes.controller";
import { DishesService } from "./dishes.service";

@Module({
  controllers: [DishesController],
  providers: [DishesService],
  exports: [DishesService],
})
export class DishesModule {}
@Module({
  imports: [DishesModule],
  controllers: [OrdersController],
  providers: [OrdersService],
})
export class OrdersModule {}

@Module({ imports: [DishesModule, OrdersModule] })
export class AppModule {}
  • Forget the export or the import and Nest refuses to start: "Nest can't resolve dependencies of the OrdersService (?)…" — read that message carefully, it tells you exactly what's missing.
  • nest generate module orders creates the folder and registers it in AppModule.

The rest of this lesson

  • Reading a dependency errorexplain
  • Shared and global modulesexplain
  • Will it start?predict
  • Fix the startup errormatch
  • Your turnwrite code
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 15 lessons of NestJS