09 / 12

How do you implement a plugin system using dynamic modules in NestJS?

Define a plugin interface where each plugin returns a DynamicModule from a static register() method. A core module accepts an array of DynamicModule objects at import time, imports and re-exports all of them, allowing the application to be extended by assembling plugin modules in AppModule.

Plugin system using dynamic modules
Benefits of the dynamic plugin pattern:
  1. 1

    Open/closed principle — add plugins without modifying the core module.

  2. 2

    Each plugin is independently configurable via its register() options.

  3. 3

    Plugins are standard NestJS modules — they can declare controllers, guards, and interceptors.

  4. 4

    Plugin providers are available to the rest of the app through CoreModule re-exports.