Building a Modular Monolith with NestJS
Use NestJS modules to build a real modular monolith: feature modules, exported providers as public APIs, events, per-module data access and boundary checks.
Published November 27, 2026
NestJS already has a module system, which makes it a natural fit for a modular monolith. But Nest modules alone do not guarantee good boundaries: it is easy to export everything, import entities across modules and end up with a tangled graph. This guide shows how to use Nest's features deliberately so each feature module behaves like an independent unit inside one deployable application.
The concept
A Nest module groups controllers and providers and declares what it exports. Only exported providers can be injected by modules that import it. That is exactly the mechanism a modular monolith needs: export a single facade service (the public API) and keep repositories, entities and internal services unexported.
For reactions between modules, @nestjs/event-emitter (or CQRS events) provides in-process publish/subscribe, so one module can announce OrderPlaced and others can react without being imported by the publisher. Data access stays inside each module: each module registers only its own entities or repositories, and other modules use the facade.
Project structure
Each business capability is a Nest module with a public facade and internal pieces.
src/
app.module.ts
modules/
catalog/
catalog.module.ts
catalog.facade.ts ← exported (public API)
dto/product-summary.dto.ts
internal/
product.entity.ts
product.repository.ts
catalog.controller.ts
checkout/
checkout.module.ts
checkout.facade.ts
events/order-placed.event.ts
internal/...
billing/
billing.module.ts
internal/order-placed.listener.ts
shared/ ← Money, idsModule graph
Nest features and how to use them
| Nest feature | Modular monolith use |
|---|---|
| @Module exports | Export only the facade; everything else private |
| imports | Explicit, acyclic dependencies between modules |
| @nestjs/event-emitter / CQRS | In-process domain events between modules |
| TypeOrmModule.forFeature / Prisma per module | Each module registers only its own entities |
| Guards / interceptors | Cross-cutting concerns without coupling modules |
| forwardRef() | A warning sign of a cycle; redesign instead |
Request flow
Practical examples
Catalog module exporting only its facade
Repositories and entities stay internal.
@Module({
imports: [TypeOrmModule.forFeature([ProductEntity])],
controllers: [CatalogController],
providers: [ProductRepository, CatalogFacade],
exports: [CatalogFacade], // the only public surface
})
export class CatalogModule {}
@Injectable()
export class CatalogFacade {
constructor(private readonly products: ProductRepository) {}
async getProductSummary(sku: string): Promise<ProductSummaryDto | null> {
const p = await this.products.findPublishedBySku(sku)
return p ? { sku: p.sku, title: p.title, pricePaise: p.pricePaise, inStock: p.stock > 0 } : null
}
}Publishing and handling a domain event
Checkout does not import Billing.
// checkout.service.ts
this.events.emit('checkout.order.placed', new OrderPlacedEvent(order.id, order.totalPaise))
// billing/internal/order-placed.listener.ts
@Injectable()
export class OrderPlacedListener {
@OnEvent('checkout.order.placed', { async: true })
async handle(evt: OrderPlacedEvent) {
await this.invoices.createFor(evt.orderId, evt.totalPaise)
}
}Boundary rule with eslint-plugin-boundaries
Allow imports of another module only via its facade or DTOs.
// .eslintrc.cjs (excerpt)
settings: { 'boundaries/elements': [{ type: 'module', pattern: 'src/modules/*', capture: ['name'] }] },
rules: {
'boundaries/entry-point': ['error', {
default: 'disallow',
rules: [{ target: ['module'], allow: ['*.facade.ts', 'dto/*.ts', 'events/*.ts', '*.module.ts'] }],
}],
}Common mistakes
- Exporting every provider from every module "to be safe".
- Importing another module's entity classes or repositories directly.
- Using forwardRef() to paper over circular module dependencies.
- A single global TypeORM entity list that lets any module query any table.
- Putting business logic in a shared module that every feature imports.
Key takeaways
- Nest modules map naturally to modular monolith modules.
- Export only a facade; keep entities and repositories internal.
- Use the event emitter or CQRS for cross-module reactions.
- Add lint or dependency rules so boundaries cannot erode.
FAQ
Is a Nest monorepo (Nx) the same as a modular monolith?
Not necessarily. Nx organises libraries and can enforce module boundaries with tags, which pairs well with a modular monolith, but the deployable can still be a single app.
Can a module later become a microservice?
Yes. Replace the facade's in-process implementation with an HTTP or message-based client and move the module into its own Nest application using @nestjs/microservices.
Should events be async?
Use async listeners for slow work and design them to be idempotent. For durability across restarts, move to a queue (BullMQ) or an outbox.