Sobes.tech
Junior — Middle

How is a module in the NestJS framework defined and used?

sobes.tech AI

Answer from AI

In NestJS, a module is the main building block of the application that groups related components such as controllers, services, and providers into a single logical unit.

A module is defined using the @Module() decorator, where you specify:

  • imports: other modules needed for the current module to function.
  • controllers: controllers that handle incoming requests.
  • providers: services and other providers that can be injected via DI.
  • exports: providers that will be available to other modules.

Example of a module:

import { Module } from '@nestjs/common';
import { UsersController } from './users.controller';
import { UsersService } from './users.service';

@Module({
  controllers: [UsersController],
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

Modules help structure the application, provide encapsulation, and manage dependencies through the built-in Dependency Injection mechanism.

How is a module in the NestJS framework defined and… - sobes.tech