A command-line tool that analyzes Angular TypeScript codebases to extract dependency injection relationships.
Target Angular Versions: 17-21
- Node.js 20.x (npm 10+)
- Angular project targeting v17-21 with a
tsconfig.jsonto discover or reference
npm install -g ng-di-graph# Analyze the whole project and print text output (default)
ng-di-graph
# Generate a Mermaid diagram and save it
ng-di-graph --format mermaid --out docs/di-graph.mmd
# Target specific files via positional filePaths
ng-di-graph src/app/app.component.ts src/app/auth.service.ts --format json
# Auto-discover the nearest tsconfig.json (no --project needed)
ng-di-graph src/app --format mermaid --out docs/di-graph.mmd
# Focus on a symbol and see who depends on it
ng-di-graph --entry UserService --direction upstream
# Override auto-discovery with an explicit tsconfig
ng-di-graph --project ./tsconfig.json --format json⨠Complete Feature Set - Production-ready dependency graph analysis for Angular applications
- π Dependency Analysis - Extract DI relationships from
@Injectable,@Component, and@Directiveclasses - π― Constructor Injection - Analyze constructor parameters with type annotations and
@Inject()tokens - β¨ Modern DI Support - Analyze dependencies from constructor injection and the
inject()function - π·οΈ Decorator Flags - Capture
@Optional,@Self,@SkipSelf, and@Hostparameter decorators - π Multiple Output Formats - Text (default), JSON (machine-readable), Mermaid (visual flowcharts)
- π¨ Entry Point Filtering - Generate sub-graphs from specific starting nodes
- π Bidirectional Analysis - Explore upstream dependencies, downstream consumers, or both
- π Circular Detection - Automatically detect and report circular dependencies
- π§Ή Angular Core Filtering - Hide
@angular/corenodes by default (--include-angular-coreto show)
- Full project, text output (default):
ng-di-graph - JSON output to stdout:
ng-di-graph --format json - Mermaid diagram to file:
ng-di-graph --format mermaid --out docs/di-graph.mmd - Filter to specific symbols:
ng-di-graph --entry AppComponent - Upstream consumers of a service:
ng-di-graph --entry UserService --direction upstream - Include decorator flags with verbose logging:
ng-di-graph --include-decorators --verbose
ng-di-graph [filePaths...] [options]
| Option | Default | Description |
|---|---|---|
filePaths |
none | Positional alias for --files; combines with --files when both are set. |
-p, --project <path> |
auto-discovered | Path to the TypeScript config file to analyze (omit to auto-discover). |
--files <paths...> |
none | Limit analysis to specific files or directories. |
-f, --format <format> |
text |
Output as text, json, or mermaid. |
-e, --entry <symbol...> |
none | Start the graph from one or more symbols. |
-d, --direction <dir> |
downstream |
downstream, upstream, or both relative to entries. |
--include-decorators |
false |
Add @Optional, @Self, @SkipSelf, @Host flags to edges. |
--include-angular-core |
false |
Include nodes that originate from @angular/core imports. |
--out <file> |
stdout | Write output to a file. |
-v, --verbose |
false |
Show detailed parsing and resolution logs. |
-h, --help |
false |
Display CLI help. |
Project: ./tsconfig.json
Scope: direction=downstream, entry=all
Files: 12 (skipped: 0)
Dependencies (A depends on B):
AppComponent
ββ UserService
UserService
ββ AuthService
ββ Logger
{
"nodes": [
{
"id": "AppComponent",
"kind": "component",
"source": { "filePath": "/path/to/src/app.component.ts", "line": 12, "column": 14 }
},
{
"id": "UserService",
"kind": "service",
"source": { "filePath": "/path/to/src/user.service.ts", "line": 8, "column": 14 }
},
{
"id": "AuthService",
"kind": "service",
"source": { "filePath": "/path/to/src/auth.service.ts", "line": 20, "column": 14 }
},
{
"id": "Logger",
"kind": "service",
"source": { "filePath": "/path/to/src/logger.service.ts", "line": 5, "column": 14 }
}
],
"edges": [
{
"from": "AppComponent",
"to": "UserService"
},
{
"from": "UserService",
"to": "AuthService",
"flags": { "optional": true, "self": false, "skipSelf": false, "host": false }
},
{
"from": "AuthService",
"to": "Logger",
"flags": { "optional": false, "self": false, "skipSelf": false, "host": false }
}
],
"circularDependencies": [["UserService", "AuthService", "UserService"]]
}Node Kinds:
service- Classes decorated with@Injectable()component- Classes decorated with@Component()directive- Classes decorated with@Directive()unknown- Could not determine decorator type
Node Source (when available):
source.filePath- Absolute file path reported by the TypeScript projectsource.line/source.column- 1-based position of the class name tokensourceis omitted forunknownnodes created from unresolved tokens
Edge Flags (when --include-decorators is used):
optional- Parameter has@Optional()decoratorself- Parameter has@Self()decoratorskipSelf- Parameter has@SkipSelf()decoratorhost- Parameter has@Host()decorator
flowchart LR
AppComponent --> UserService
UserService --> AuthService
Mermaid diagrams can be rendered in GitHub/GitLab markdown, viewed in the Mermaid Live Editor, or converted to images with CLI tools.
- Test planning: surface dependencies to decide what to mock.
- Impact analysis: list consumers before changing a service.
- Documentation: add Mermaid diagrams to READMEs or architecture docs.
Releases are automated via Release Please; tag pushes run lint β typecheck β test β build β pack β publish. Maintainer runbook: see docs/release/ci-publish.md.
The CLI reports clear failures and continues past unparseable files; rerun with --verbose for detailed logs.
Contributions are welcome! Please see our Contributing Guide for development setup, code quality standards, and the pull request process.
MIT License - See LICENSE file for details