NestJS module design: connect feature boundaries and provider dependencies
NestJS modules group related controllers and providers into feature boundaries and declare which dependencies other modules can use. Controllers receive HTTP requests; providers such as services hold application work; module metadata registers both with Nest’s dependency-injection container. As a feature grows, design explicit module boundaries rather than only splitting files by role.
NestJS modules group related controllers and providers into feature boundaries and declare which dependencies other modules can use. Controllers receive HTTP requests; providers such as services hold application work; module metadata registers both with Nest’s dependency-injection container. As a feature grows, design explicit module boundaries rather than only splitting files by role.
Organize a feature module
A Nest application has at least a root module and commonly imports feature modules from it. For example, an OrdersModule can register OrdersController and OrdersService, while AppModule lists OrdersModule in imports. In @Module(), controllers lists the controllers to create and providers lists injectable dependencies.
Keep controllers focused on receiving route requests and passing validated input onward. A service can own business rules or repository access, separating HTTP handling from feature logic. This is an organizational pattern, not a required file naming or folder layout.
Share providers deliberately
Providers are singleton-scoped by default, matching the application lifecycle. To use a provider in another module, export it from its owning module and import that module where needed. Registering the same provider separately in several modules can create separate instances. For a stateful service this may cause inconsistent behavior, so prefer one owning registration and export it.
Constructor injection usually declares a provider class as a parameter type; Nest resolves the registered token and supplies an instance. TypeScript interfaces and type aliases disappear at runtime and cannot serve as default tokens. For an interface-based implementation, register a runtime token such as a string or Symbol and inject it with @Inject(). Sharing a token constant helps prevent mismatched names and collisions.
Making every service global can hide imports and increase coupling. Reserve global modules for truly common infrastructure; use explicit imports and exports for ordinary feature sharing so dependencies remain visible.
Trace the module connection
A possible order feature could look like this:
OrdersModule registers OrdersController and OrdersService, and exports the service only if another feature needs it.
AppModule imports OrdersModule to include it in the application.
ReportsModule imports OrdersModule if it needs the order service. It does not register OrdersService again in its own providers.
If injection fails after export, first check that the consuming module imports the owning module. Then verify the provider is in the owner’s providers and, when shared, its exports. If the declared type is an interface, confirm the runtime token matches the one passed to @Inject(). Incorrect module imports or duplicate provider registrations can also make the dependency graph differ from expectations.
Configure and verify
Check the project’s Nest version in its package manifest or with npm ls @nestjs/core. Use CLI generation commands supported by the installed CLI.
Review the feature module’s controllers, providers, and exports, and connect it through the root or consumer module’s imports.
Start the application and confirm there is no dependency-resolution error. Nest builds the provider graph during bootstrap.
Call the feature’s HTTP route and verify that the controller returns the expected result through its service. A missing export or import can cause an injection error at startup or leave the provider unavailable to the consuming module.
Confirm startup and requests still work when only the related modules are composed. If a change breaks resolution, restore the explicit module boundary before adding duplicate providers or unnecessary global visibility.
A feature module is more than a folder convention: it defines provider ownership and public access. Explicit imports and exports make the dependency graph easier to review and reduce coupling or state surprises caused by global access and duplicate instances.
References
[NestJS: Modules](https://docs.nestjs.com/modules)
[NestJS: Providers](https://docs.nestjs.com/providers)
[NestJS: Controllers](https://docs.nestjs.com/controllers)
[NestJS: Custom providers](https://docs.nestjs.com/fundamentals/custom-providers)