How to Create a Module in NestJS: Simple Guide
In NestJS, create a module by defining a class decorated with
@Module that groups related components. Use the imports, controllers, and providers arrays inside the decorator to organize your app's structure.Syntax
The @Module decorator defines a module class in NestJS. It accepts an object with these main properties:
- imports: Other modules to include
- controllers: Controllers to handle requests
- providers: Services or providers for business logic
- exports: Providers to share with other modules
This structure helps organize your app into logical parts.
typescript
import { Module } from '@nestjs/common'; @Module({ imports: [], controllers: [], providers: [], exports: [], }) export class YourModuleName {}
Example
This example shows a simple UsersModule with a controller and a service. It demonstrates how to group related parts in one module.
typescript
import { Module } from '@nestjs/common'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; @Module({ controllers: [UsersController], providers: [UsersService], }) export class UsersModule {} // users.controller.ts import { Controller, Get } from '@nestjs/common'; import { UsersService } from './users.service'; @Controller('users') export class UsersController { constructor(private readonly usersService: UsersService) {} @Get() findAll() { return this.usersService.findAll(); } } // users.service.ts import { Injectable } from '@nestjs/common'; @Injectable() export class UsersService { findAll() { return ['Alice', 'Bob', 'Charlie']; } }
Output
GET /users response: ["Alice", "Bob", "Charlie"]
Common Pitfalls
Common mistakes when creating modules include:
- Forgetting to add controllers or providers to the
@Moduledecorator arrays, so they won't be recognized. - Not exporting providers if they need to be used in other modules.
- Creating modules without importing dependent modules, causing missing dependencies.
Always ensure your module's metadata correctly lists all parts.
typescript
/* Wrong: Missing provider in module */ import { Module } from '@nestjs/common'; import { UsersController } from './users.controller'; @Module({ controllers: [UsersController], // providers: [UsersService], // Forgot this }) export class UsersModule {} /* Right: Include provider */ import { Module } from '@nestjs/common'; import { UsersController } from './users.controller'; import { UsersService } from './users.service'; @Module({ controllers: [UsersController], providers: [UsersService], }) export class UsersModule {}
Quick Reference
Remember these tips when creating modules:
- Use
@Moduledecorator to define modules. - List all controllers and providers inside the decorator.
- Export providers if other modules need them.
- Import other modules your module depends on.
- Keep modules focused on a single feature or domain.
Key Takeaways
Use the @Module decorator to group related controllers and providers in NestJS.
Always list controllers and providers inside the module metadata for them to work.
Export providers if they should be accessible by other modules.
Import dependent modules to share functionality across your app.
Keep modules focused and organized for easier maintenance.