Skip to content

How to map dependencies between files in a JavaScript or TypeScript project

Find out which files depend on which, spot circular imports and see the architecture: tools like Madge and dependency-cruiser, IDE features and manual techniques.

October 13, 2026 · 2 min read · RepoGrasp team

A dependency map shows which modules import which. It's the skeleton of a codebase's architecture, and it answers practical questions: what breaks if I change this file? Is the UI layer importing the database directly? Why does this test pull in half the app?

Why dependency maps matter

  • Impact analysis. Everything that imports a module, directly or indirectly, can be affected by a change to it.
  • Architecture checks. Layered designs only work if dependencies point one way.
  • Circular dependencies. Cycles cause confusing initialisation bugs in JavaScript, where a module can be undefined when first used.
  • Bundle size. An unexpected import can pull a large dependency into the browser.

Tool 1: Madge

npx madge --extensions ts,tsx src/
npx madge --circular --extensions ts,tsx src/
npx madge --image graph.svg src/index.ts

Madge lists dependencies, finds cycles and draws graphs (the image needs Graphviz installed). Point it at an entry file to see only what that entry reaches.

Tool 2: dependency-cruiser

dependency-cruiser builds the graph and lets you write rules, such as "nothing in src/ui may import from src/db", then fails CI when a rule is broken. It turns a diagram into an enforced architecture.

Tool 3: your IDE

"Find all references" on an export shows its direct dependents. VS Code's call hierarchy shows callers of a function. These are exact but one level at a time. See IDE features for reading code.

Tool 4: monorepo tooling

In a monorepo, nx graph or your workspace tool shows dependencies between packages. Start there before mapping files inside a package.

Doing it by hand

For a quick map of one feature:

  1. List the files in the feature folder.
  2. For each, read its imports and note which point outside the folder.
  3. Search the rest of the codebase for imports of the feature's files.

You'll get the feature's dependencies and dependents, which is usually enough.

Reading the map

  • Hubs (files many others import) are risky to change and worth testing well.
  • Leaves (files that import nothing internal) are safe places to start contributing.
  • Long chains suggest layering; short, tangled webs suggest the boundaries have eroded.
  • Imports that go "up" from low-level code to high-level code usually indicate a design problem.

Imports aren't the whole story

Static import maps miss runtime connections: dependency injection, event emitters, plugin registries, string-based lookups and HTTP calls between services. Two files can depend on each other through an event name without either importing the other. For behaviour, you need traces as well as maps.

With RepoGrasp

The RepoGrasp sample demo shows answers alongside a dependency graph, highlighting the path an answer follows. On real repositories, you can ask "what depends on lib/pricing.ts, directly and indirectly?" and get cited results, then check them with madge or "find all references".

Try the demo.