The @Module decorator helps organize your NestJS app by grouping related parts together. It tells NestJS what controllers, providers, and other modules belong to this group.
Module decorator and metadata in NestJS
@Module({
imports: [],
controllers: [],
providers: [],
exports: []
})
export class YourModule {}The @Module decorator takes an object with metadata properties.
imports is for other modules you want to use inside this module.
@Module({
controllers: [CatsController],
providers: [CatsService]
})
export class CatsModule {}@Module({
imports: [DatabaseModule],
controllers: [UsersController],
providers: [UsersService],
exports: [UsersService]
})
export class UsersModule {}This example shows a simple CatsModule with a controller and a service. The controller uses the service to return a list of cats when you visit the '/cats' route.
import { Module } from '@nestjs/common'; import { CatsController } from './cats.controller'; import { CatsService } from './cats.service'; @Module({ controllers: [CatsController], providers: [CatsService] }) export class CatsModule {} // cats.controller.ts import { Controller, Get } from '@nestjs/common'; import { CatsService } from './cats.service'; @Controller('cats') export class CatsController { constructor(private readonly catsService: CatsService) {} @Get() findAll() { return this.catsService.findAll(); } } // cats.service.ts import { Injectable } from '@nestjs/common'; @Injectable() export class CatsService { private readonly cats = ['Tom', 'Garfield', 'Felix']; findAll() { return this.cats; } }
Always list controllers and providers inside the @Module decorator to make them part of the module.
Use exports to share providers with other modules.
Modules help keep your app organized like folders in a filing cabinet.
@Module groups related parts of your app together.
It uses metadata like imports, controllers, providers, and exports.
This helps NestJS know how to build and connect your app pieces.
Practice
@Module decorator in NestJS?Solution
Step 1: Understand the role of @Module
The@Moduledecorator organizes related parts of the app such as controllers and providers.Step 2: Compare options
The options describing database connections, HTTP routing, or UI styling are unrelated to the main role of@Module.Final Answer:
To group related components like controllers and providers together -> Option BQuick Check:
@Module groups controllers & providers [OK]
- Thinking @Module sets up database connections
- Confusing @Module with routing decorators
- Assuming @Module styles the UI
AppController and a provider named AppService?Solution
Step 1: Check correct property names and syntax
The@Moduledecorator expects arrays forcontrollersandprovidersproperties.Step 2: Validate each option
Singular property names like 'controller'/'provider', missing array brackets, or parentheses instead of square brackets are incorrect. The correct syntax uses plural 'controllers'/'providers' with arrays.Final Answer:
@Module({ controllers: [AppController], providers: [AppService] }) -> Option DQuick Check:
controllers & providers must be arrays [OK]
- Using singular property names like controller or provider
- Not wrapping controllers/providers in arrays
- Using parentheses instead of square brackets
AppModule.imports.length?
@Module({
imports: [CommonModule, DatabaseModule],
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}Solution
Step 1: Identify the imports array length
Theimportsarray contains two modules:CommonModuleandDatabaseModule.Step 2: Calculate the length property
Since there are two items,AppModule.imports.lengthequals 2.Final Answer:
2 -> Option AQuick Check:
imports array length = 2 [OK]
- Assuming imports is empty or undefined
- Confusing imports with controllers or providers
- Forgetting to count all items in the array
@Module({
imports: CommonModule,
controllers: [AppController],
providers: [AppService]
})
export class AppModule {}Solution
Step 1: Check the imports property type
Theimportsproperty must be an array, but here it is a single module without brackets.Step 2: Verify other properties
controllersandprovidersare correctly arrays. The module class does not require a constructor by default.Final Answer:
imports should be an array, not a single module -> Option AQuick Check:
imports must be array [OK]
- Using single module without array in imports
- Thinking controllers can't be arrays
- Assuming constructor is mandatory
SharedService from SharedModule so other modules can use it. Which is the correct way to configure SharedModule to allow this?Solution
Step 1: Understand sharing services via exports
To share a service, it must be both provided and exported in the module metadata.Step 2: Analyze options
Sharing requires listing the service in bothproviders(to provide it) andexports(to make it available to importers).importsis for modules, not services. Missing either providers or exports prevents sharing.Final Answer:
@Module({ providers: [SharedService], exports: [SharedService] }) -> Option CQuick Check:
providers AND exports needed [OK]
- Forgetting to export the service
- Trying to import a service instead of providing it
- Only exporting without providing the service
Ready to prove it?
Pass the test and this pattern moves unknown
