In NestJS, lazy loading is an optimization technique that improves application startup speed and runtime efficiency. By using lazy loading, modules are only loaded and instantiated when a user requests a specific route, rather than loading all modules at application startup.
Conceptual Understanding
- Lazy Loading: Certain modules or components are loaded only when first accessed, rather than being loaded entirely at application startup.
- Execution Context: Refers to the environment in which a module is loaded, including the state of the dependency injection container and other relevant configurations.
Implementation Steps
Creating the Main Module and Routes
First, create a main module and configure the routes.
// main.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RouterModule } from '@nestjs/core';
@Module({
imports: [
RouterModule.register([
{ path: 'users', module: 'UsersModule' }, // Note the use of string reference
{ path: 'posts', module: 'PostsModule' }, // Note the use of string reference
]),
],
controllers: [AppController],
providers: [AppService],
})
export class MainModule {}Creating Lazy-Loaded Modules
Create the user module (users.module.ts) and the posts module (posts.module.ts), and mark them for lazy loading.
// users.module.ts
import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';
@Module({
controllers: [UsersController],
providers: [UsersService],
})
export class UsersModule {}
// posts.module.ts
import { Module } from '@nestjs/common';
import { PostsController } from './posts.controller';
import { PostsService } from './posts.service';
@Module({
controllers: [PostsController],
providers: [PostsService],
})
export class PostsModule {}Creating Controllers and Services
Create the corresponding controllers and services.
// users.controller.ts
import { Controller, Get } from '@nestjs/common';
@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 'List of all users';
}
}
// posts.controller.ts
import { Controller, Get } from '@nestjs/common';
@Controller('posts')
export class PostsController {
constructor(private readonly postsService: PostsService) {}
@Get()
findAll() {
return this.postsService.findAll();
}
}
// posts.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class PostsService {
findAll() {
return 'List of all posts';
}
}Configuring the Main Module
Register the routes for the lazy-loaded modules in the main module.
// main.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { RouterModule } from '@nestjs/core';
@Module({
imports: [
RouterModule.register([
{ path: 'users', module: () => import('./users/users.module').then(m => m.UsersModule) },
{ path: 'posts', module: () => import('./posts/posts.module').then(m => m.PostsModule) },
]),
],
controllers: [AppController],
providers: [AppService],
})
export class MainModule {}



