README
MaDGe - Module Dependency Graph
Madge is a developer tool for generating a visual graph of your module dependencies, finding circular dependencies, and give you other useful info. Joel Kemp's awesome dependency-tree is used for extracting the dependency tree.
- Works for JavaScript (AMD, CommonJS, ES6 modules) and CSS preprocessors (Sass, Stylus)
- NPM installed dependencies are excluded by default (can be enabled in config)
- All core Node.js modules (assert, path, fs, etc) are excluded
- Recurse into child dependencies to get a complete dependency tree of a file
Read the changelog for latest changes.
Examples
Graph generated from madge's own code and dependencies.
A graph with circular dependencies. Blue has dependencies, green has no dependencies, and red has circular dependencies.
See it in action
Installation
$ npm -g install madge
Graphviz (optional)
Only required if you want to generate the visual graphs using Graphviz.
Mac OS X
$ brew install graphviz || port install graphviz
Ubuntu
$ apt-get install graphviz
API
madge(path: string|array|object, config: object)
path
is a single file or directory, or an array of files/directories to read. A predefined tree can also be passed in as an object.
config
is optional and should be the configuration to use.
Returns a
Promise
resolved with the Madge instance object.
Functions
.obj()
Returns an
Object
with all dependencies.
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.obj());
});
.circular()
Returns an
Array
with all modules that has circular dependencies.
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.circular());
});
.depends()
Returns an
Array
with all modules that depends on a given module.
const madge = require('madge');
madge('path/to/app.js').then((res) => {
console.log(res.depends());
});
.dot()
Returns a
Promise
resolved with a DOT representation of the module dependency graph.
const madge = require('madge');
madge('path/to/app.js')
.then((res) => res.dot())
.then((output) => {
console.log(output);
});
.image(imagePath: string)
Write the graph as an image to the given image path. The image format to use is determined from the file extension. Returns a
Promise
resolved with a full path to the written image.
const madge = require('madge');
madge('path/to/app.js')
.then((res) => res.image('path/to/image.svg'))
.then((writtenImagePath) => {
console.log('Image written to ' + writtenImagePath);
});
});
Configuration
Property | Type | Default | Description |
---|---|---|---|
baseDir |
String | null | Base directory to use instead of the default |
includeNpm |
Boolean | false | If node_modules should be included |
fileExtensions |
Array | ['js'] | Valid file extensions used to find files in directories |
showFileExtension |
Boolean | false | If file extension should be included in module name |
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 |
layout |
String | dot | Layout to use in the graph |
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 |
nodeColor |
String | #c6c5fe | Default node color to use in the graph |
noDependencyColor |
String | #cfffac | Color to use for nodes with no dependencies |
cyclicNodeColor |
String | #ff6c60 | Color to use for circular dependencies |
edgeColor |
String | #757575 | Edge color to use in the graph |
graphVizOptions |
Object | false | Custom GraphViz options |
graphVizPath |
String | null | Custom GraphViz path |
detectiveOptions |
Object | false | Custom detective options for dependency-tree |
Note that when running the CLI it's possible to use a runtime configuration file. The config should placed in
.madgerc
in your project or home folder. Look here for alternative locations for the file. Here's an example:
{
"showFileExtension": true,
"fontSize": "10px",
"graphVizOptions": {
"G": {
"rankdir": "LR"
}
}
}
CLI
Examples
List dependencies from a single file
$ madge path/src/app.js
List dependencies from multiple files
$ madge path/src/foo.js path/src/bar.js
List dependencies from all *.js files found in a directory
$ madge path/src
List dependencies from multiple directories
$ madge path/src/foo path/src/bar
List dependencies from all *.js and *.jsx files found in a directory
$ madge --extensions js,jsx path/src
Finding circular dependencies
$ madge --circular path/src/app.js
Show modules that depends on a given module
$ madge --depends 'wheels' path/src/app.js
Excluding modules
$ madge --exclude '^(foo|bar)