A NestJS module should make ownership visible: which capabilities it provides, which providers it owns, and what other modules may depend on. A module that imports half the application is usually hiding unclear responsibility rather than solving it.
Start from the use case
Keep controllers focused on transport concerns: parse validated input, call an application service, and map the result to the HTTP contract. Put business rules in a service or use-case provider that can be tested without starting an HTTP server. Put persistence behind the project’s established data-access boundary; do not create a repository abstraction if the codebase has no need for it.
Register providers in the module that owns them. Export only the providers another module genuinely needs, and import that module where the dependency is used. Avoid reaching into another module’s private files or injecting a controller. Circular module imports often indicate that two responsibilities have not been separated cleanly.
Validate at the boundary
DTO validation is runtime behavior, not just a TypeScript type. Reject malformed input before domain work begins and return consistent client errors. In update routes, distinguish an omitted field from an explicit null if those values mean different things. Authorization belongs near the operation that knows the resource and actor.
Let the module describe its public surface
Nest modules group controllers and providers. Controllers receive requests, and providers own behavior that can be injected. Nest makes a provider available to another module when the declaring module exports it and the consumer imports that module. A provider can therefore remain private to a feature even if it lives in the same process. NestJS module reference
Start a module with one route that demonstrates its responsibility. In the health example, the controller owns the transport response. If the feature later needs a database check, give it a provider with an explicit dependency and a bounded readiness check. In tests, override that provider through a testing module and exercise the real controller. That lets the team add infrastructure without turning every test into a full server boot. NestJS testing utilities
Worked example
This NestJS module demonstrates a controller boundary. Import it into the application module; a global API prefix may change the final URL.
tsimport { Controller, Get, Module } from "@nestjs/common"; @Controller("health") class HealthController { @Get() read() { return { status: "ok" }; } } @Module({ controllers: [HealthController] }) export class HealthModule {}




Community
Discussion
Loading comments…