A monolith can remain a good deployment unit while its internal structure improves. Refactoring into modules is useful when changes repeatedly cross unclear ownership boundaries, not because a diagram looks cleaner with more boxes.
Find a seam with evidence
Look for a capability with its own vocabulary, data rules, and change history. Map who calls it, what data it owns, and which invariants must remain true. Prefer a boundary with a small number of consumers. Before moving files, add characterization tests around externally visible behavior and record current database or API contracts.
Create the module interface first, then move implementation behind it. Avoid a “big bang” rewrite that changes behavior, storage, and deployment at the same time. During a database transition, use expand-and-contract steps so old and new code can coexist during rollout. Remove the old path only after traffic and data checks show it is unused.
Keep the feedback loop short
Ship one vertical slice and observe errors, latency, and support reports. Preserve rollback options for schema and routing changes. If the new boundary requires more coordination than it removes, reconsider the split. A separate process adds network failure, deployment, monitoring, and on-call responsibilities.
Measure whether the refactor reduces change coupling or improves independent ownership. The strongest result is a smaller blast radius and clearer contracts, not the number of modules created.
Make the first cut at a public capability
A cross-feature import that reaches into another feature's database adapter is harder to change safely than a call to one exported application capability. Pick a behavior you can describe without naming its tables, such as fetching a published article for a reader. Put its current authorization policy behind that interface, and move implementation details only after callers use the boundary. NestJS's import and export rules give the module a concrete way to enforce that API. NestJS modules
Keep a regression test around the public behavior before moving files. Test a published article, a restricted one, and a missing slug. Compare responses and side effects across the refactor. The diff can be smaller and easier to roll back when a new module boundary has one observable purpose. A text search can reveal candidates, but the repository's architecture rule should decide which imports actually violate ownership.
Worked example
Use the search to locate potential boundary violations, then inspect each match. The repository boundary check is stronger evidence than assuming every textual match is an invalid import.
shrg -n 'from ["\x27].*(features|apps)/' packages --glob '*.ts' bun run lint:boundaries




Community
Discussion
Loading comments…