Monorepo Dependency Graph for Safer AI-Assisted Code Changes
Part of the Code Knowledge Graphs guide. Start with Knowledge Graph for Code Explained: A Practical Guide
Suppose you're changing a return type in shared-auth, the assistant has opened the implementation, the package's tests are passing, and you still haven't checked the apps that consume it. That's the question I want a monorepo dependency graph to answer before the edit grows.
Start with the graph your workspace already has. Find the consumers of the shared package, inspect the work that depends on it, then open the relevant callers, including the ones outside the package you're editing.
I care about that last step. A build dependency tells me where to investigate. It doesn't tell me whether a particular caller handles the new return type correctly.
Pick the graph that answers your question
A monorepo dependency graph connects things inside one repository, but those things might be projects, source modules, build targets, or individual tasks. Read the node labels before drawing conclusions about the edges.
| Visualizer | What it maps | What to check |
|---|---|---|
| Nx graph | Projects, with a separate task view | Inspect which files create a project dependency. |
Turborepo --graph |
Tasks and their dependencies | Compare the graph with turbo.json and actual package scripts. |
| Bazel query | BUILD targets and dependency edges | Check the target label and query universe. |
| dependency-cruiser | Source-module dependencies, including JavaScript and TypeScript | Validate edges against your configured rules. |
| Madge | Module dependencies and circular dependencies | Check warnings about files it couldn't resolve. |
For the broader model, see how file imports and function callers differ in a code dependency graph.
The Nx, Turborepo and Bazel commands below assume an existing workspace configured for the relevant tool. Run them at its root, substitute your own project names and labels, inspect the result, and keep the workspace's normal checks.
Nx: inspect projects, then tasks
npx runs the Nx commands below. List the workspace's projects and open a focused view. shared-auth is a placeholder for an actual project name.
npx nx show projects
npx nx graph --focus=shared-auth
nx graph opens the project graph in your browser. The focus option keeps the selected project's ancestors and descendants, so it includes dependencies in both directions, gives you a smaller view to inspect, and shouldn't be read as a list of consumers only. See the Nx graph command reference.
Click an edge and inspect the source files behind it. Pick a consumer you already know about and make sure it's present.
nx graph writes the graph to a file when you pass --file, which is useful when you want to save the JSON, attach an HTML view to a review, compare it with the checkout, or share it with someone who isn't running your workspace.
npx nx graph --focus=shared-auth --file=auth-graph.json
npx nx graph --focus=shared-auth --file=auth-graph.html
The HTML export comes with supporting assets. Keep those with it. In the browser, Nx also offers a PNG export from the graph controls.
Nx's task graph is a separate view. nx build accepts --graph to show the task graph Nx would execute for the app.
Here my-app stands for an app that consumes shared-auth:
npx nx build my-app --graph
Check whether shared-auth:build appears among the prerequisites of my-app:build, then compare that view with the project graph you just opened.
This is where I'd check whether a shared package's build is actually connected to the consuming app's build. A project edge and a task prerequisite answer different questions.
Turborepo: visualize the tasks you're asking to run
Turborepo distinguishes its Package Graph from its Task Graph. The package manager supplies package relationships, turbo.json supplies task relationships, nodes in the task graph represent tasks, and edges describe their dependencies.
From a configured Turborepo workspace:
turbo run build --graph
turbo run build --graph=build.html
The first command prints a DOT graph to standard output. The second writes an HTML visualization.
Both forms are documented in the turbo run reference.
Look for the changed package's build, follow its dependent tasks, compare their ordering with turbo.json, and check the matching scripts in each package. The current docs flag a graphing limitation where a possible task node can appear even when that package has no corresponding script.
That makes the script check worth doing. Don't turn a node in this visualization into a claim that a test exists or ran.
Bazel: query reverse dependencies and render them
Bazel's rdeps query finds reverse dependencies.
It searches inside the transitive closure of a chosen universe. Without a depth argument, it follows the full reverse dependency chain within that scope.
With Bazel and Graphviz's dot available, you can render that result:
bazel query 'rdeps(//..., //pkg)' --output graph | dot -Tsvg -o dependents.svg
Replace //pkg before running this unless your repo really defines that target. Under Bazel's label rules, it abbreviates //pkg:pkg.
It doesn't mean every target in the package. Use //pkg:all if you want all rule targets there. A query around a specific existing library might instead look like this:
bazel query 'rdeps(//..., //packages/shared:shared)' --output graph | dot -Tsvg -o dependents.svg
Here //... selects the main repository's rule targets as universe roots, rdeps finds consumers within their dependency closure, --output graph produces DOT, and dot writes the SVG. Check the target-pattern reference before changing that scope.
One limit matters here. bazel query gives an unconfigured target graph, which can conservatively include dependencies from different configurations, so treat the picture as a set of candidates, inspect the configuration relevant to your change, and avoid calling it an action graph.
When you need to inspect imports inside a package
For a source-module view, dependency-cruiser supports graph output and dependency rules.
Run this from the package you're inspecting; the dependency-cruiser README documents this sequence:
pnpm add -D dependency-cruiser
npx dependency-cruiser --init
npx dependency-cruiser src --include-only "^src" --output-type dot | dot -T svg > dependency-graph.svg
dependency-cruiser's --init command asks a few questions and creates a configuration file, so review the generated rules, choose the source directory you intend to inspect, and use the graph to investigate connections inside that scope. The --include-only "^src" filter drops imports dependency-cruiser couldn't resolve, so check those separately with npx dependency-cruiser src --output-type err, which reports each one as not-to-unresolvable.
dependency-cruiser runs the configured analysis in the final command, uses src as its input, keeps only modules under that directory, and emits DOT for Graphviz to render. Adapt both paths to your package layout.
On a TypeScript package, run npx dependency-cruiser --info before --init and check that typescript and .ts are ticked. Release 18.7.0 supports typescript from 2.0 up to, but not including, 7.0, and with no supported compiler --init only prints a warning, after which the final command emits a DOT graph with no modules and still exits 0.
Madge is another option for module graphs and circular dependencies.
The Madge README also documents warnings for skipped files. Resolve those before trusting an apparently isolated module.
A seven-step mapping pass before an AI edit
Use this when mapping an unfamiliar monorepo or checking an existing graph. You don't need to build a new graph service to do it.
- Choose the level. Start with the package or project being changed. Go to source modules or callers when the question concerns an implementation detail.
- Identify the inputs. Check manifests and source imports, inspect the build configuration, include relevant generated-code inputs, and note which relationships still need manual inspection.
- Check edge direction. Separate what the target uses from what uses the target. Follow consumers through intermediate packages.
- Normalize boundaries. Match graph nodes to real workspace names and paths. Resolve aliases or unexpected project names before interpreting missing edges.
- Filter around the change. Keep the target and relevant consumers visible. Record the scope so a filtered view isn't mistaken for the entire repo.
- Validate a known path. Pick a consumer you can verify in source. If it is absent, investigate resolution or configuration before narrowing the test plan.
- Regenerate and record the revision. Recheck after changing dependency configuration. Attach the graph's scope and revision to the review.
I want the assistant's plan to name the changed package, explain the consumer paths it inspected, identify any gaps, and tie its proposed checks to those paths. That's something a reviewer can check against the code.
Add caller context without confusing it with the build graph
I build Pharaoh, a hosted MCP server for TypeScript, JavaScript, Python, C and C++ repositories. It maps functions, files, modules, dependencies, endpoints and cron jobs so an assistant can ask about connections in the code.
For example, get_module_context returns a module's exports and external callers, search_functions locates implementations, get_blast_radius follows production callers, and pharaoh_recon can combine several questions in one call.
This is captured output from the public cal.diy fork maintained by Pharaoh-so, revision 54343aa6, from get_blast_radius for checkAdminOrOwner at depth 3, trimmed to two of the returned modules.
### api/v2
- apps/api/v2/src/modules/atoms/controllers/atoms.conferencing-apps.controller.ts: AtomsConferencingAppsController.listUserInstalledConferencingApps (3 hops)
- apps/api/v2/src/modules/atoms/controllers/atoms.event-types.controller.ts: AtomsEventTypesController.getAtomEventTypeApp (2 hops)
- apps/api/v2/src/modules/atoms/services/conferencing-atom.service.ts: ConferencingAtomsService.getUserConferencingApps (2 hops)
- apps/api/v2/src/modules/atoms/services/event-types-atom.service.ts: EventTypesAtomService.getEventTypesAppIntegration
### app-store/_utils
- packages/app-store/_utils/getConnectedApps.ts: getConnectedApps
Those paths give you specific callers to open, including consumers outside the auth module, so read what each caller expects, then compare that with the proposed change.
The scope matters. Pharaoh's graph follows the default branch, and a live repository is re-mapped on every push there (every repository is live during the trial and on Pro). It doesn't include another branch or uncommitted edits. get_blast_radius excludes test-file callers.
For PR review, PR Guard is available on Pro and checks new dead code, breaking changes, exact duplicates, and blast radius. It publishes a neutral, advisory check by default.
For a fuller review process, see the blast radius of a change across packages.
Give the assistant a scope you can review
Before handing over an edit, save the graph view, note the checkout revision, list the consumers you checked, and spell out any missing relationships. Ask the assistant to read those callers before proposing its test plan.
For client-specific workflows, see running Claude Code inside a monorepo.
There's also a guide to Cursor in a monorepo.
To add Pharaoh's caller context, install its GitHub App and connect your client.
Connect your repository and client.
In a Claude Code terminal, the setup command is npx @pharaoh-so/mcp. Once connected, ask for the target module's external callers, compare them with your checkout, inspect the implementation, and keep any uncertainty in the review.
For your next shared-package change, start with one known consumer. Make sure your map can find it before you use that map to exclude anything from testing.
Monorepo dependency graph FAQ
What does a monorepo dependency graph show?
It can show relationships between projects, source modules, build targets, or tasks. Read the node labels and graph scope before interpreting the edges.
Which tools can visualize a monorepo dependency graph?
Nx maps projects and tasks, Turborepo maps packages and tasks, Bazel query maps build targets, and dependency-cruiser or Madge map source-module dependencies.
How are an Nx project graph and task graph different?
The project graph shows project relationships. The task graph shows the tasks Nx would execute and their prerequisites, such as whether shared-auth:build is required by my-app:build.
How should I validate a dependency graph before an AI-assisted code change?
Choose the graph level, check edge direction and boundaries, validate a known consumer in source, record the filtered scope and revision, and investigate missing relationships before narrowing the test plan.
Article Record
ReferencesPrimary sources used for this article
Update HistoryMeaningful revisions to this article
- Added four frequently asked questions to help readers better understand dependency graphs and make safer AI-assisted code changes.