How to Import Module in NestJS: Simple Guide
In NestJS, you import a module by adding it to the
imports array inside the @Module decorator of another module. This allows you to use the exported providers and controllers from the imported module in your current module.Syntax
To import a module in NestJS, you use the @Module decorator and add the module class to the imports array. This tells NestJS to include that module's exported components in the current module.
@Module: Decorator that defines a module.imports: Array where you list other modules to import.ModuleClass: The class of the module you want to import.
typescript
import { Module } from '@nestjs/common'; import { ModuleClass } from './module-class'; @Module({ imports: [ModuleClass], controllers: [], providers: [], }) export class CurrentModule {}
Example
This example shows how to import a UsersModule into an AppModule. The UsersModule exports a service that AppModule can use.
typescript
import { Module, Injectable, Controller, Get } from '@nestjs/common'; // UsersService definition @Injectable() export class UsersService { getUsers() { return ['Alice', 'Bob', 'Charlie']; } } // UsersModule with a simple service @Module({ providers: [UsersService], exports: [UsersService], }) export class UsersModule {} // AppService uses UsersService @Injectable() export class AppService { constructor(private usersService: UsersService) {} listUsers() { return this.usersService.getUsers(); } } // AppController to show users @Controller() export class AppController { constructor(private appService: AppService) {} @Get('users') getUsers() { return this.appService.listUsers(); } } // AppModule imports UsersModule @Module({ imports: [UsersModule], controllers: [AppController], providers: [AppService], }) export class AppModule {}
Output
["Alice", "Bob", "Charlie"]
Common Pitfalls
Common mistakes when importing modules in NestJS include:
- Not exporting providers or services in the imported module, so they are not available to the importing module.
- Forgetting to add the module to the
importsarray. - Importing the same module multiple times unnecessarily.
Always export what you want to share and import the module properly.
typescript
/* Wrong: UsersService not exported, so AppModule can't use it */ @Module({ providers: [UsersService], // exports: [UsersService], // missing export }) export class UsersModule {} /* Right: Export UsersService to share it */ @Module({ providers: [UsersService], exports: [UsersService], }) export class UsersModule {}
Quick Reference
Remember these key points when importing modules in NestJS:
- Use
@Moduledecorator withimportsarray. - Export providers/services you want to share with other modules.
- Import modules only once to avoid redundancy.
- Imported modules give access to their exported providers and controllers.
Key Takeaways
Import modules by listing them in the @Module decorator's imports array.
Export providers in a module to make them available to importing modules.
Always add the module class to imports to use its exported components.
Avoid forgetting exports or imports to prevent runtime errors.
Import modules only once to keep your app organized and efficient.