What is madge?
Madge is a JavaScript library that helps you visualize and analyze the dependency graph of your project. It can be used to identify circular dependencies, generate dependency graphs, and more.
What are madge's main functionalities?
Generate Dependency Graph
This feature allows you to generate a dependency graph of your project. The code sample demonstrates how to use Madge to analyze the dependencies in a given project directory and output the result as an object.
const madge = require('madge');
madge('path/to/your/project').then((res) => {
console.log(res.obj());
});
Identify Circular Dependencies
Madge can identify circular dependencies in your project. The code sample shows how to use Madge to find and log any circular dependencies in the specified project directory.
const madge = require('madge');
madge('path/to/your/project').then((res) => {
console.log(res.circular());
});
Generate Graphviz DOT File
Madge can generate a Graphviz DOT file for visualizing the dependency graph. The code sample demonstrates how to generate and log the DOT representation of the dependency graph.
const madge = require('madge');
madge('path/to/your/project').then((res) => {
return res.dot();
}).then((output) => {
console.log(output);
});
Create an Image of the Dependency Graph
Madge can create an image file of the dependency graph. The code sample shows how to generate an image of the dependency graph and save it as 'graph.png'.
const madge = require('madge');
madge('path/to/your/project').then((res) => {
return res.image('graph.png');
}).then((writtenImagePath) => {
console.log('Image written to ' + writtenImagePath);
});
Other packages similar to madge
dependency-cruiser
Dependency-cruiser is a tool to analyze and visualize dependencies in JavaScript and TypeScript projects. It offers similar functionalities to Madge, such as detecting circular dependencies and generating dependency graphs. However, it provides more customization options and supports more file types.
webpack-bundle-analyzer
Webpack-bundle-analyzer is a plugin and CLI utility that represents the size of webpack output files with an interactive zoomable treemap. While it focuses more on bundle size analysis rather than dependency graphs, it provides insights into the structure of your project and helps optimize bundle sizes.
plato
Plato is a JavaScript source code visualization, static analysis, and complexity tool. It generates visual reports of your codebase, including dependency graphs. Compared to Madge, Plato offers a broader range of static analysis features but may not be as focused on dependency graph generation.
MaDGe - Module Dependency Graph
Create graphs from your CommonJS or AMD module dependencies. Could also be useful for finding circular dependencies in your code. Tested on Node.js and RequireJS projects. Dependencies are calculated using static code analysis with UglifyJS.
Example
Here's a very simple example of a generated image.
- blue = has dependencies
- green = has no dependencies
- red = has circular dependencies
Installation
To install as a library:
$ npm install madge
To install the command-line tool:
$ sudo npm -g install madge
Graphviz (optional)
Only required if you want to generate the visual graphs using Graphviz.
Mac OS X
$ sudo port install graphviz
Ubuntu
$ sudo apt-get install graphviz
API
Coming soon ..
CLI
Usage: madge [options] <file|dir ...>
Options:
-h, --help output usage information
-V, --version output the version number
-f, --format <name> format to parse (amd/cjs)
-o, --output <type> output format (plain/json)
-s, --summary show summary of all dependencies
-c, --circular show circular dependencies
-d, --depends <id> show modules that depends on the given id
-x, --exclude <regex> a regular expression for excluding modules
-t, --dot output graph in the DOT language
-i, --image <filename> write graph to file as a PNG image
-l, --layout <name> layout engine to use for image graph (dot/neato/fdp/sfdp/twopi/circo)
-b, --break-on-error break on parse errors & missing modules
-n, --no-colors skip colors in output and images
-r, --read skip scanning folders and read JSON from stdin
Examples:
Finding circular dependencies
$ madge --circular /path/src
Show modules that depends on a given module
$ madge --depends 'wheels' /path/src
Excluding modules
$ madge --exclude '^test$|^app\.build$|^tests|^text\!' /path/src
Save graph as a PNG image (graphviz required)
$ madge --image graph.png /path/src && open graph.png
Save graph as a DOT file for further processing (graphviz required)
$ madge --dot /path/src > graph.gv
Pipe predefined results (the example image was produced with the following command)
$ cat << EOF | madge --read --image example.png
{
"a": ["b", "c", "d"],
"b": ["c"],
"c": [],
"d": ["a"]
}
EOF
Running tests
$ npm test
License
(The MIT License)
Copyright (c) 2012 Patrik Henningsson <patrik.henningsson@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.