UNPKG

10.6 kBMarkdownView Raw
1<p align="center">
2 <img alt="madge" src="http://pahen.github.io/madge/logo.svg" width="320">
3</p>
4
5<p align="center">
6 <img alt="Last version" src="https://img.shields.io/github/tag/pahen/madge.svg?style=flat-square" />
7 <a href="https://travis-ci.org/pahen/madge">
8 <img alt="Build Status" src="http://img.shields.io/travis/pahen/madge/master.svg?style=flat-square" />
9 </a>
10 <a href="https://david-dm.org/pahen/madge">
11 <img alt="Dependency status" src="http://img.shields.io/david/pahen/madge.svg?style=flat-square" />
12 </a>
13 <a href="https://david-dm.org/pahen/madge#info=devDependencies">
14 <img alg="Dev Dependencies status" src="http://img.shields.io/david/dev/pahen/madge.svg?style=flat-square" />
15 </a>
16 <a href="https://www.npmjs.org/package/madge">
17 <img alg="NPM Status" src="http://img.shields.io/npm/dm/madge.svg?style=flat-square" />
18 </a>
19 <a href="https://paypal.me/pahen">
20 <img alt="Donate" src="https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square" />
21 </a>
22</p>
23
24<p align="center">
25 <a href="https://www.patreon.com/bePatron?u=16473892">
26 <img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" alt"Become a Patron" width="170" />
27 </a>
28</p>
29
30**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](https://github.com/mrjoelkemp/node-dependency-tree) is used for extracting the dependency tree.
31
32
33* Works for JavaScript (AMD, CommonJS, and ES6 modules)
34* Also works for CSS preprocessors (Sass, Stylus, and Less)
35* NPM installed dependencies are excluded by default (can be enabled)
36* All core Node.js modules (assert, path, fs, etc) are excluded
37* Will traverse child dependencies automatically
38
39Read the [changelog](CHANGELOG.md) for latest changes.
40
41## Examples
42
43> Graph generated from madge's own code and dependencies.
44
45<a href="http://pahen.github.io/madge/madge.svg">
46 <img src="http://pahen.github.io/madge/madge.svg" width="888"/>
47</a>
48
49> A graph with circular dependencies. Blue has dependencies, green has no dependencies, and red has circular dependencies.
50
51<a href="http://pahen.github.io/madge/simple.svg">
52 <img src="http://pahen.github.io/madge/simple.svg" width="300"/>
53</a>
54
55## See it in action
56
57<a href="https://asciinema.org/a/M5tS7FrwKo8N3KUaCVO41J7iW?autoplay=1">
58 <img src="https://asciinema.org/a/M5tS7FrwKo8N3KUaCVO41J7iW.png" width="590"/>
59</a>
60
61# Installation
62
63```sh
64$ npm -g install madge
65```
66
67## Graphviz (optional)
68
69> [Graphviz](http://www.graphviz.org/) is only required if you want to generate visual graphs (e.g. in SVG or DOT format).
70
71### Mac OS X
72
73```sh
74$ brew install graphviz || port install graphviz
75```
76
77### Ubuntu
78
79```sh
80$ apt-get install graphviz
81```
82
83# API
84
85## madge(path: string|array|object, config: object)
86
87> `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.
88
89> `config` is optional and should be the [configuration](#configuration) to use.
90
91> Returns a `Promise` resolved with the Madge instance object.
92
93## Functions
94
95#### .obj()
96
97> Returns an `Object` with all dependencies.
98
99```javascript
100const madge = require('madge');
101
102madge('path/to/app.js').then((res) => {
103 console.log(res.obj());
104});
105```
106
107#### .warnings()
108
109> Returns an `Object` of warnings.
110
111```javascript
112const madge = require('madge');
113
114madge('path/to/app.js').then((res) => {
115 console.log(res.warnings());
116});
117```
118
119#### .circular()
120
121> Returns an `Array` of all modules that has circular dependencies.
122
123```javascript
124const madge = require('madge');
125
126madge('path/to/app.js').then((res) => {
127 console.log(res.circular());
128});
129```
130
131#### .depends()
132
133> Returns an `Array` of all modules that depend on a given module.
134
135```javascript
136const madge = require('madge');
137
138madge('path/to/app.js').then((res) => {
139 console.log(res.depends('lib/log.js'));
140});
141```
142
143#### .orphans()
144
145> Return an `Array` of all modules that no one is depending on.
146
147```javascript
148const madge = require('madge');
149
150madge('path/to/app.js').then((res) => {
151 console.log(res.orphans());
152});
153```
154
155#### .dot()
156
157> Returns a `Promise` resolved with a DOT representation of the module dependency graph.
158
159```javascript
160const madge = require('madge');
161
162madge('path/to/app.js')
163 .then((res) => res.dot())
164 .then((output) => {
165 console.log(output);
166 });
167```
168
169#### .image(imagePath: string)
170
171> Write the graph as an image to the given image path. The [image format](http://www.graphviz.org/content/output-formats) to use is determined from the file extension. Returns a `Promise` resolved with a full path to the written image.
172
173```javascript
174const madge = require('madge');
175
176madge('path/to/app.js')
177 .then((res) => res.image('path/to/image.svg'))
178 .then((writtenImagePath) => {
179 console.log('Image written to ' + writtenImagePath);
180 });
181```
182
183# Configuration
184
185Property | Type | Default | Description
186--- | --- | --- | ---
187`baseDir` | String | null | Base directory to use instead of the default
188`includeNpm` | Boolean | false | If shallow NPM modules should be included
189`fileExtensions` | Array | ['js'] | Valid file extensions used to find files in directories
190`excludeRegExp` | Array | false | An array of RegExp for excluding modules
191`requireConfig` | String | null | RequireJS config for resolving aliased modules
192`webpackConfig` | String | null | Webpack config for resolving aliased modules
193`tsConfig` | String\|Object | null | TypeScript config for resolving aliased modules - Either a path to a tsconfig file or an object containing the config
194`layout` | String | dot | Layout to use in the graph
195`rankdir` | String | LR | Sets the [direction](https://graphviz.gitlab.io/_pages/doc/info/attrs.html#d:rankdir) of the graph layout
196`fontName` | String | Arial | Font name to use in the graph
197`fontSize` | String | 14px | Font size to use in the graph
198`backgroundColor` | String | #000000 | Background color for the graph
199`nodeShape` | String | box | A string specifying the [shape](https://graphviz.gitlab.io/_pages/doc/info/attrs.html#k:shape) of a node in the graph
200`nodeStyle` | String | rounded | A string specifying the [style](https://graphviz.gitlab.io/_pages/doc/info/attrs.html#k:style) of a node in the graph
201`nodeColor` | String | #c6c5fe | Default node color to use in the graph
202`noDependencyColor` | String | #cfffac | Color to use for nodes with no dependencies
203`cyclicNodeColor` | String | #ff6c60 | Color to use for circular dependencies
204`edgeColor` | String | #757575 | Edge color to use in the graph
205`graphVizOptions` | Object | false | Custom Graphviz [options](https://graphviz.gitlab.io/_pages/doc/info/attrs.html)
206`graphVizPath` | String | null | Custom Graphviz path
207`detectiveOptions` | Object | false | Custom `detective` options for [dependency-tree](https://github.com/dependents/node-dependency-tree)
208`dependencyFilter` | Function | false | Function called with a dependency filepath (exclude substree by returning false)
209
210> 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](https://github.com/dominictarr/rc#standards) for alternative locations for the file. Here's an example:
211
212```json
213{
214 "fontSize": "10px",
215 "graphVizOptions": {
216 "G": {
217 "rankdir": "LR"
218 }
219 }
220}
221```
222
223# CLI
224
225## Examples
226
227> List dependencies from a single file
228
229```sh
230$ madge path/src/app.js
231```
232
233> List dependencies from multiple files
234
235```sh
236$ madge path/src/foo.js path/src/bar.js
237```
238
239> List dependencies from all *.js files found in a directory
240
241```sh
242$ madge path/src
243```
244
245> List dependencies from multiple directories
246
247```sh
248$ madge path/src/foo path/src/bar
249```
250
251> List dependencies from all *.js and *.jsx files found in a directory
252
253```sh
254$ madge --extensions js,jsx path/src
255```
256
257> Finding circular dependencies
258
259```sh
260$ madge --circular path/src/app.js
261```
262
263> Show modules that depends on a given module
264
265```sh
266$ madge --depends wheels.js path/src/app.js
267```
268
269> Show modules that no one is depending on
270
271```sh
272$ madge --orphans path/src/
273```
274
275> Excluding modules
276
277```sh
278$ madge --exclude '^(foo|bar)\.js$' path/src/app.js
279```
280
281> Save graph as a SVG image (requires [Graphviz](#graphviz-optional))
282
283```sh
284$ madge --image graph.svg path/src/app.js
285```
286
287> Save graph as a [DOT](http://en.wikipedia.org/wiki/DOT_language) file for further processing (requires [Graphviz](#graphviz-optional))
288
289```sh
290$ madge --dot path/src/app.js > graph.gv
291```
292
293> Using pipe to transform tree (this example will uppercase all paths)
294
295```sh
296$ madge --json path/src/app.js | tr '[a-z]' '[A-Z]' | madge --stdin
297```
298
299# Debugging
300
301> 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.
302
303```sh
304$ madge --debug path/src/app.js
305```
306
307# Running tests
308
309```sh
310$ npm test
311```
312
313# FAQ
314
315## Missing dependencies?
316
317It could happen that the files you're not seeing have been skipped due to errors or that they can't be resolved. Run madge with the `--warning` option to see skipped files. If you need even more info run with the `--debug` option.
318
319## What's the "Error: write EPIPE" when exporting graph to image?
320
321Ensure you have [installed Graphviz](#graphviz-optional). If you're running Windows, note that Graphviz is not added to the `PATH` variable during install. You should add the folder of `gvpr.exe` (typically `%Graphviz_folder%/bin`) to the `PATH` variable manually.
322
323## The image produced by madge is very hard to read, what's wrong?
324
325Try running madge with a different layout, here's a list of the ones you can try:
326
327* **dot** "hierarchical" or layered drawings of directed graphs. This is the default tool to use if edges have directionality.
328
329* **neato** "spring model'' layouts. This is the default tool to use if the graph is not too large (about 100 nodes) and you don't know anything else about it. Neato attempts to
330minimize a global energy function, which is equivalent to statistical multi-dimensional scaling.
331
332* **fdp** "spring model'' layouts similar to those of neato, but does this by reducing forces rather than working with energy.
333
334* **sfdp** multiscale version of fdp for the layout of large graphs.
335
336* **twopi** radial layouts, after Graham Wills 97. Nodes are placed on concentric circles depending their distance from a given root node.
337
338* **circo** circular layout, after Six and Tollis 99, Kauffman and Wiese 02. This is suitable for certain diagrams of multiple cyclic structures, such as certain telecommunications networks.
339
340# License
341
342MIT License