---
id: 48
type: blog
title: "NestJS Modules Are the Boundary"
slug: "nestjs-modules-are-the-boundary"
canonical_url: "https://docs.phatysd.me/content/nestjs-modules-are-the-boundary"
status: published
revision: 1
published_at: "2026-09-19T17:51:06.174Z"
updated_at: "2026-09-19T17:51:06.174Z"
hierarchy_path: "NestJS > Engineering Journal > Architecture"
topics:
  - "NestJS"
  - "Modules"
  - "Dependency injection"
  - "Architecture"
tags:
  - "nestjs"
  - "typescript"
  - "modules"
  - "dependency-injection"
  - "architecture"
related:
  - slug: "nestjs-feature-module-runbook"
    title: "NestJS Feature Module Runbook"
    type: docs
    canonical_url: "https://docs.phatysd.me/content/nestjs-feature-module-runbook"
---

# 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

- [NestJS modules](https://docs.nestjs.com/modules)
- [NestJS validation](https://docs.nestjs.com/techniques/validation)
- [NestJS configuration](https://docs.nestjs.com/techniques/configuration)
- [NestJS lifecycle events](https://docs.nestjs.com/fundamentals/lifecycle-events)