A growing NestJS application becomes difficult to change when every module can reach into every other module’s providers and data. The answer is not necessarily microservices; first make ownership and dependency direction clear inside the existing deployment.
Organize around business capabilities
Group controllers, application behavior, and persistence access around a capability with a clear owner. Controllers should translate HTTP requests and responses, while application services implement use cases. Keep shared infrastructure small and explicit. A shared module that becomes a dumping ground only moves coupling to a different folder.
Export a provider only when another module needs that capability. Avoid importing internal files through long relative paths or creating circular dependencies to share a function. If two modules need the same behavior, decide whether one owns it or whether it is truly a shared domain capability.
Make contracts and transactions visible
Use DTO schemas for transport validation and keep internal domain types distinct when their rules differ. Database constraints enforce invariants across modules and background jobs. Keep multi-step writes transactional, and use an outbox when a database change must reliably trigger asynchronous work.
Add import-boundary checks and tests that exercise module composition. Extract a module into a separate service only when independent scaling, deployment, security, or ownership is worth the operational cost. Modular architecture scales when a change has a small, understandable blast radius.
Choose a small module API
Nest exports are the visible API of a module to other application modules. Make the exported provider express a capability such as loading an approved article, rather than the names of tables or transport handlers. A consumer can then depend on behavior that remains stable if storage changes. Keep repository construction and write policies inside the owning module. NestJS module reference
When a dependency begins to point both ways, start with the business action crossing the boundary. For an article submission, the publishing feature can own the transition and emit a recorded event for delivery. The notification feature consumes the event without calling a controller or reopening the article transaction. Test module wiring and the shared event contract separately from the business rules. NestJS testing utilities
Worked example
The returned title is deliberately illustrative. In a real module, inject the repository and policy rather than importing another controller or reaching across feature internals.
tsimport { Injectable, Module } from "@nestjs/common"; @Injectable() export class ArticleReader { readPublicTitle() { return "Example article"; } } @Module({ providers: [ArticleReader], exports: [ArticleReader] }) export class ArticlesModule {}




Community
Discussion
Loading comments…