Visualizing internal dependencies with Madge

When working on large Angular projects, managing dependencies between files and modules can become a significant challenge. Madge is a developer tool that visualizes and analyzes module dependencies in JavaScript applications, making it an excellent companion for Angular developers.

Here is how to give it a try:

  1. Install madge: npm install -g madge
  2. In the root folder of your project, run: npx madge src/main.ts --ts-config tsconfig.json --image ./deps.png

This command will go through your entire application and create a dependency graph as an image in deps.png:

This can be used to identify circular dependencies, visualize relationships between your different components and services, and identify spaghetti code scenarios.

Note that Madge has other specific commands, such as one to detect circular dependencies:
npx madge --circular src/main.ts --ts-config tsconfig.json

Or that one to detect dead code (code that is never referred to anywhere in your app):

npx madge --orphans src/main.ts --ts-config tsconfig.json

You can find more information on Madge’s NPM page here.

Alain Chautard

Alain is a Google Developer Expert in Web Technologies, Angular, and Google Maps. His daily mission is to help development teams adopt Angular and build at scale with the framework. He has taught Angular on all six continents! A world traveler and photographer, Alain is also an international conference speaker, and a published author of several video courses.