Install Madge via npm
masterInstall Madge globally using npm to use it as a CLI tool.
npm -g install madgerepository·master·Indexed 27 days ago
https://github.com/pahen/madgeA developer tool for generating visual module dependency graphs, identifying circular dependencies, and analyzing module relationships in JavaScript and CSS preprocessor files. It provides both a CLI and a programmatic API to extract dependency data, find orphans and leaves, and export graphs in DOT, SVG, or image formats using Graphviz.
Install Madge globally using npm to use it as a CLI tool.
npm -g install madgeGraphviz is required if you want to generate visual graphs in formats like SVG or DOT.
Mac OS X:
brew install graphviz || port install graphvizUbuntu:
apt-get install graphvizMadge uses dependency-tree and precinct. You can fine-tune how it handles specific syntax via detectiveOptions in your configuration.
Enable mixed ES6 + CommonJS imports:
{
"detectiveOptions": {
"es6": { "mixedImports": true }
}
}Ignore import type in ES6 + Flow:
{
"detectiveOptions": {
"es6": { "skipTypeImports": true }
}
}Ignore import in type annotations in TypeScript:
{
"detectiveOptions": {
"ts": { "skipTypeImports": true }
}
}Ignore dynamic imports in TypeScript:
{
"detectiveOptions": {
"ts": { "skipAsyncImports": true },
"tsx": { "skipAsyncImports": true }
}
}Configuration can be provided via a .madgerc file in your project or home folder, or directly in your package.json under a madge key.
| Property | Type | Default | Description |
|---|---|---|---|
baseDir | String | null | Base directory to use instead of the default |
includeNpm | Boolean | false | If shallow NPM modules should be included |
fileExtensions | Array | ['js'] | Valid file extensions used to find files |
excludeRegExp | Array | false | An array of RegExp for excluding modules |
requireConfig | String | null | RequireJS config for resolving aliased modules |
webpackConfig | String | null | Webpack config for resolving aliased modules |
tsConfig | String|Object | null | TypeScript config (path or object) for resolving aliased modules |
layout | String | dot | Layout to use in the graph |
rankdir | String | LR | Sets the direction of the graph layout |
fontName | String | Arial | Font name to use in the graph |
fontSize | String | 14px | Font size to use in the graph |
backgroundColor | String | #000000 | Background color for the graph |
nodeShape | String | box | Shape of a node |
nodeStyle | String | rounded | Style of a node |
nodeColor | String | #c6c5fe | Default node color |
noDependencyColor | String | #cfffac | Color for nodes with no dependencies |
cyclicNodeColor | String | #ff6c60 | Color for circular dependencies |
edgeColor | String | #757575 | Edge color |
graphVizOptions | Object | false | Custom Graphviz options |
graphVizPath | String | null | Custom Graphviz path |
detectiveOptions | Object | false | Custom detective options |
dependencyFilter | Function | false | Function called with a dependency filepath (return false to exclude subtrees) |
If you encounter Error: write EPIPE while attempting to export a graph to an image, it is likely because Graphviz is not installed or not accessible in your system's PATH.
On Windows:
Graphviz does not automatically add itself to the PATH variable during installation. You must manually add the folder containing gvpr.exe (typically %Graphviz_folder%/bin) to your system's PATH environment variable.
If you receive the error Graphviz not built with triangulation library when using the sfdp layout on macOS via Homebrew, you must install the gts library explicitly. Run the following commands:
brew uninstall graphviz
brew install gts
brew install graphvizIf you encounter problems or missing dependencies, use the --debug flag to enable detailed output. If files are missing, you can also use the --warning flag to see which files were skipped.
madge --debug path/src/app.jsThe madge(path, config) function is the primary entry point for the API.
path: A single file, a directory, an array of files/directories, or a predefined tree object.config: An optional configuration object.Returns a Promise that resolves to a Madge instance object.
const madge = require('madge');
madge('path/to/app.js').then((res) => {
// Use the Madge instance methods here
});Once the Madge instance is resolved, use these methods to extract information:
.obj(): Returns an Object containing all dependencies..warnings(): Returns an Object of warnings (e.g., skipped files)..circular(): Returns an Array of modules with circular dependencies..circularGraph(): Returns an Object containing only circular dependencies..depends(modulePath): Returns an Array of all modules that depend on the specified module..orphans(): Returns an Array of modules that no one is depending on..leaves(): Returns an Array of modules that have no dependencies..dot([circularOnly: boolean]): Returns a Promise resolving to a DOT representation string. Set circularOnly to true to include only circular dependencies..image(imagePath, [circularOnly: boolean]): Writes the graph as an image to imagePath. The format is determined by the file extension. Returns a Promise resolving to the full path of the written image..svg(): Returns a Promise resolving to a Buffer containing the XML SVG representation.When initializing Madge, you can pass a config object to override default settings. Key configuration options include:
| Key | Default | Description |
|---|---|---|
baseDir | null | Base directory for resolution |
excludeRegExp | false | RegExp to exclude files |
fileExtensions | ['js'] | Array of file extensions to include |
includeNpm | false | Whether to include npm modules in the graph |
tsConfig | null | Path to a tsconfig.json file (string) or parsed config object |
webpackConfig | null | Webpack configuration object |
rankdir | 'LR' | Graphviz direction (e.g., 'LR', 'TB') |
layout | 'dot' | Graphviz layout engine |
nodeColor | '#c6c5fe' | Color of the nodes |
edgeColor | '#757575' | Color of the edges |
cyclicNodeColor | '#ff6c60' | Color of nodes involved in circular dependencies |
graphVizPath | false | Path to the Graphviz executable |
If the generated image is difficult to read, try using a different Graphviz layout engine. Available layouts include:
fdp designed for large graphs.--circular flag. If circular dependencies are found, the CLI will exit with code 1.