Blog · NestJS / Engineering Journal / Architecture

NestJS Modules Are the Boundary

NestJS Modules Are the Boundary

NestJS can look like a collection of decorators when you first open a project. The more useful mental model is a graph of feature modules. Each module says which controllers and providers belong to a capability and which dependencies are intentionally imported or exported.

A module is an ownership statement

OrdersModule is not just a folder name. It answers questions: which routes serve orders, which provider owns the use case, which repository is behind it, and which service is safe to expose to another feature?

That makes the module graph a design tool. A root module that imports many feature modules is easy to explain. A root module that owns every provider is a signal that feature boundaries have started to disappear.

Dependency injection helps when the boundary stays honest

Injecting a service makes the controller easy to test and lets infrastructure change without rewriting the route contract. It does not make every provider a global utility. If a provider belongs to one capability, keep it private to that module unless another module has a clear reason to use it.

Explicit exports are useful because they turn accidental coupling into a reviewable decision.

Validation is the first line, not the whole policy

A global ValidationPipe gives the application a consistent request boundary. Whitelisting and rejecting unexpected properties can prevent a client from silently sending fields that the DTO never intended to accept.

But a valid DTO is not an authorized command. Authorization, ownership, and business invariants still belong in guards and application services. The cleanest NestJS code makes those responsibilities visible instead of hiding them in a large controller.

Operations reveal whether the architecture is real

Modules and providers look tidy in a diagram. Production tests whether the boundaries work: does configuration fail early when required values are missing? Do shutdown hooks close connections? Is API documentation exposed only where the security policy allows it? Can a feature be tested without booting unrelated features?

Those questions are why I now treat the module graph and lifecycle hooks as part of the service's operational design, not only its code organization.

The practical rule

Create a module when a capability has its own routes, use cases, and data boundary. Keep controllers thin. Validate at the edge. Export only what a different capability genuinely needs. If a module becomes a dumping ground, split it before the dependency graph becomes the architecture nobody wants to change.

References

Agent access

Choose a representation.

Published content only
Human pageYou are reading the human-facing canonical page.
Current page

Both machine-readable links refer to this same published item and preserve its identity, relationships, freshness, and body.