Template - API
WelcomePrerequisitesNodeJS
  • Welcome!
    • About me
  • Prerequisites
    • Installation and Database Setup
    • Installing Node.js on the Server
  • NodeJS
    • 🆓Getting Started with the Free Template
    • 🎗️Premium Template - REST API
      • Structure
      • Middlewares and Guards
      • Core Modules
        • Prisma with NestJS
        • Swagger with NestJS
      • Dynamic Module
        • Create or Import Module
  • Modules
    • 🎗️Websockets
    • 🎗️Mailer
    • 🎗️Upload
Powered by GitBook
On this page
  • How Does It Work ?
  • Benefits of This Design
  1. NodeJS
  2. Premium Template - REST API

Dynamic Module

A DynamicModule in your NestJS API is a module that can be loaded at runtime without needing to be explicitly specified in the imports array of a class or another module.

his provides significant flexibility by allowing you to add or remove functionality without modifying the main source code or redeploying the application.


How Does It Work ?

A DynamicModule follows a logic that enables NestJS to discover and load a module as soon as it is moved into a specific directory like src/modules. Here are the key steps and components involved

1. Automatic Detection

The core of the process relies on a mechanism that scans the file system (such as using fs in Node.js), detects new folders/modules in a specified directory (like src/modules), and dynamically loads them. This is often implemented with a service like DynamicModulesLoaderService, which scans the directory at runtime, analyzes the module structure, and automatically injects the module.

2. Dynamic Loading Service

The DynamicModulesLoaderService plays a central role in this process. This service is responsible for:

  • Scanning the module folder: It traverses the target directory (src/modules) looking for dynamic submodules.

  • Validating the modules: Each submodule is validated to ensure it follows a specific structure (for example, containing a module.ts file).

  • Loading the modules: Once detected, the module is dynamically loaded and injected into the NestJS application lifecycle without needing to manually modify the imports array of the existing modules.

3. Custom Decorators

There may be custom decorators like @DynamicModule() or interfaces that help identify that a module can be dynamically loaded. These decorators can be used to mark a module so that it is supported by the dynamic loader.

4. Example of the Loading Process

Imagine moving a module called payment into the src/modules folder. The DynamicModulesLoaderService detects this new module by scanning the directory. If the module contains a payment.module.ts file that follows the NestJS convention, the module is then automatically loaded and available for use within the application, without requiring you to manually add it to the app.module.ts.


Benefits of This Design

  1. Scalability: You can easily add or remove features by adding or removing dynamic modules without impacting the main codebase.

  2. Isolation: Each module is self-contained and can be managed independently from others.

  3. Flexibility: Modules can be developed by separate teams and added to the application without direct coordination with the core team.

  4. Simplified Import Management: Avoids the need to manually update the imports in main classes, especially in large-scale applications.

In summary, the DynamicModule in your API makes the architecture more flexible and modular by automating the process of loading newly added or modified modules.

PreviousSwagger with NestJSNextCreate or Import Module

Last updated 8 months ago

🎗️