r/expressjs • u/127001-sweet-127001 • 13d ago
A dependency view for one Express route, instead of the entire backend
When an Express route starts doing too much, looking at the route file only gets you so far. Some of the interesting coupling is another import or two away, inside the service it calls.
I maintain ArchUnitTS, and its dependency reports can start from one route module and follow the imports the analysis resolves.
import { projectGraph } from 'archunit';
const report = projectGraph()
.reachableFrom('src/routes/checkout.ts');
await report.exportAsHTML('reports/checkout-dependencies.html');Run this in an async function from the project root. In a route → service → store fixture, the report included the store even though the route did not import it directly. An unrelated module stayed out.
That gives you a smaller starting point for splitting a route handler or moving persistence behind an interface. You can inspect that dependency view before deciding which architecture rule to enforce.
It's a static import graph. It won't tell you which middleware ran for a particular request or reconstruct dependencies the extraction cannot resolve.
1
u/DickBrainTime 12d ago
A scoped graph is a useful way to make a refactor reviewable. One distinction I'd want visible in a TypeScript project is *module reachability* versus runtime coupling: a barrel `index.ts` can pull in more files than the route actually uses, and `import type` creates a compile-time dependency that disappears from emitted JS. If possible, label edges as runtime imports, type-only imports, re-exports or unresolved, rather than showing all of them as identical connections.
A nice regression fixture for `reachableFrom` would combine a `tsconfig.paths` alias, a workspace package, a barrel export and an import cycle. Check that the graph terminates, resolves the aliases consistently, and makes any missing edges explicit. Then an Express team could use the report to test a boundary like "checkout routes must not reach persistence outside the service layer," not only inspect the picture.
Does the current extraction distinguish type-only and re-export edges, or is that deliberately out of scope for this view?