Modules


Introduction to Modules

In NestJS, a Module is a class annotated with the @Module() decorator. It acts as the “container” that organizes your controllers and services into a cohesive functional block.

Every application has at least one Root Module (usually AppModule), which Nest uses to build the Application Graph—the internal map of how all your classes depend on each other.



1. The @Module() Properties ⚙️

The decorator takes an object with four essential properties:

PropertyPurpose
providersThe services/helpers that belong to this module.
controllersThe API routes defined in this module that need to be instantiated.
importsOther modules whose exported providers you need to use here.
exportsThe providers from this module that other modules are allowed to use.


2. Feature Modules 📁

As your app grows, you should group related code into Feature Modules. For example, everything related to “Cats” should live in a CatsModule.

// cats/cats.module.ts
@Module({
  controllers: [CatsController],
  providers: [CatsService],
  exports: [CatsService] // Share CatsService with other modules
})
export class CatsModule {}