Madge
Create graphs from your CommonJS, AMD or ES6 module dependencies
README
I've worked with Madge on my free time for the last couple of years and it's been a great experience. It started as an experiment but turned out to be a very useful tool for many developers. I have many ideas for the project and it would definitely be easier to dedicate more time to it with some financial support 🙏
Regardless of your contribution, thanks for your support!
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
- ```sh
- npm -g install madge
- ```
Graphviz (optional)
Graphviz is only required if you want to generate visual graphs (e.g. in SVG or DOT format).
Mac OS X
- ```sh
- brew install graphviz || port install graphviz
- ```
Ubuntu
- ```sh
- 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.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.obj());
- });
- ```
.warnings()
Returns an Object of warnings.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.warnings());
- });
- ```
.circular()
Returns an Array of all modules that have circular dependencies.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.circular());
- });
- ```
.circularGraph()
Returns an Object with only circular dependencies.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.circularGraph());
- });
- ```
.depends()
Returns an Array of all modules that depend on a given module.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.depends('lib/log.js'));
- });
- ```
.orphans()
Return an Array of all modules that no one is depending on.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.orphans());
- });
- ```
.leaves()
Return an Array of all modules that have no dependencies.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js').then((res) => {
- console.log(res.leaves());
- });
- ```
.dot([circularOnly: boolean])
Returns a Promise resolved with a DOT representation of the module dependency graph. Set circularOnly to only include circular dependencies.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js')
- .then((res) => res.dot())
- .then((output) => {
- console.log(output);
- });
- ```
.image(imagePath: string, [circularOnly: boolean])
Write the graph as an image to the given image path. Set circularOnly to only include circular dependencies. The image format to use is determined from the file extension. Returns aPromise resolved with a full path to the written image.
- ```javascript
- 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);
- });
- ```
.svg()
Return a Promise resolved with the XML SVG representation of the dependency graph as a Buffer.
- ```javascript
- const madge = require('madge');
- madge('path/to/app.js')
- .then((res) => res.svg())
- .then((output) => {
- console.log(output.toString());
- });
- ```
Configuration
.madgerc
- ```json
- {
- "fontSize": "10px",
- "graphVizOptions": {
- "G": {
- "rankdir": "LR"
- }
- }
- }
- ```
package.json
- ```json
- {
- "name": "foo",
- "version": "0.0.1",
- ...
- "madge": {
- "fontSize": "10px",
- "graphVizOptions": {
- "G": {
- "rankdir": "LR"
- }
- }
- }
- }
- ```
CLI
Examples
List dependencies from a single file
- ```sh
- madge path/src/app.js
- ```
List dependencies from multiple files
- ```sh
- madge path/src/foo.js path/src/bar.js
- ```
List dependencies from all *.js files found in a directory
- ```sh
- madge path/src
- ```
List dependencies from multiple directories
- ```sh
- madge path/src/foo path/src/bar
- ```
List dependencies from all .js and .jsx files found in a directory
- ```sh
- madge --extensions js,jsx path/src
- ```
Finding circular dependencies
- ```sh
- madge --circular path/src/app.js
- ```
Show modules that depends on a given module
- ```sh
- madge --depends wheels.js path/src/app.js
- ```
Show modules that no one is depending on
- ```sh
- madge --orphans path/src/
- ```
Show modules that have no dependencies
- ```sh
- madge --leaves path/src/
- ```
Excluding modules
- ```sh
- madge --exclude '^(foo|bar)\.js$' path/src/app.js
- ```
Save graph as a SVG image (requires Graphviz)
- ```sh
- madge --image graph.svg path/src/app.js
- ```
Save graph with only circular dependencies
- ```sh
- madge --circular --image graph.svg path/src/app.js
- ```
- ```sh
- madge --dot path/src/app.js > graph.gv
- ```
Using pipe to transform tree (this example will uppercase all paths)
- ```sh
- madge --json path/src/app.js | tr '[a-z]' '[A-Z]' | madge --stdin
- ```
Debugging
To enable debugging output if you encounter problems, run madge with the --debug option then throw the result in a gist when creating issues on GitHub.
- ```sh
- madge --debug path/src/app.js
- ```
Running tests
- ```sh
- npm install
- npm test
- ```
Creating a release
- ```sh
- npm run release
- ```
FAQ
Missing dependencies?
Using both Javascript and Typescript in your project?
Using mixed import syntax in the same file?
- ```json
- {
- "detectiveOptions": {
- "es6": {
- "mixedImports": true
- }
- }
- }
- ```
- ```json
- {
- "detectiveOptions": {
- "ts": {
- "mixedImports": true
- }
- }
- }
- ```
How to ignore import type statements in ES6 + Flow?
- ```json
- {
- "detectiveOptions": {
- "es6": {
- "skipTypeImports": true
- }
- }
- }
- ```
How to ignore import in type annotations in TypeScript?
- ```json
- {
- "detectiveOptions": {
- "ts": {
- "skipTypeImports": true
- }
- }
- }
- ```
Mixing TypesScript and Javascript imports?
- ```json
- {
- "compilerOptions": {
- "module": "commonjs",
- "allowJs": true
- }
- }
- ```
What's the "Error: write EPIPE" when exporting graph to image?
How do I fix the "Graphviz not built with triangulation library" error when using sfdp layout?
- ```sh
- brew uninstall graphviz
- brew install gts
- brew install graphviz
- ```
The image produced by madge is very hard to read, what's wrong?
Credits
Contributors
Donations ❤️