Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including @zhenyu925/madge with all npm packages installed. Try it out:

var madge = require("@zhenyu925/madge")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

@zhenyu925/madge v3.6.2

Create graphs from module dependencies.


Last version Build Status Dependency status Donate

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, and ES6 modules)
  • Also works for CSS preprocessors (Sass, Stylus, and Less)
  • NPM installed dependencies are excluded by default (can be enabled)
  • All core Node.js modules (assert, path, fs, etc) are excluded
  • Will traverse child dependencies automatically

Read the changelog for latest changes.

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!


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


$ 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

$ brew install graphviz || port install graphviz


$ apt-get install graphviz


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.



Returns an Object with all dependencies.

const madge = require('madge');

madge('path/to/app.js').then((res) => {


Returns an Object of warnings.

const madge = require('madge');

madge('path/to/app.js').then((res) => {


Returns an Array of all modules that has circular dependencies.

const madge = require('madge');

madge('path/to/app.js').then((res) => {


Returns an Array of all modules that depend on a given module.

const madge = require('madge');

madge('path/to/app.js').then((res) => {


Return an Array of all modules that no one is depending on.

const madge = require('madge');

madge('path/to/app.js').then((res) => {


Returns a Promise resolved with a DOT representation of the module dependency graph.

const madge = require('madge');

    .then((res) =>
    .then((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');

    .then((res) => res.image('path/to/image.svg'))
    .then((writtenImagePath) => {
        console.log('Image written to ' + writtenImagePath);


Return a Promise resolved with the XML SVG representation of the dependency graph as a Buffer.

const madge = require('madge');

    .then((res) => res.svg())
    .then((output) => {


baseDirStringnullBase directory to use instead of the default
includeNpmBooleanfalseIf shallow NPM modules should be included
fileExtensionsArray['js']Valid file extensions used to find files in directories
excludeRegExpArrayfalseAn array of RegExp for excluding modules
requireConfigStringnullRequireJS config for resolving aliased modules
webpackConfigStringnullWebpack config for resolving aliased modules
tsConfigString|ObjectnullTypeScript config for resolving aliased modules - Either a path to a tsconfig file or an object containing the config
layoutStringdotLayout to use in the graph
rankdirStringLRSets the direction of the graph layout
fontNameStringArialFont name to use in the graph
fontSizeString14pxFont size to use in the graph
backgroundColorString#000000Background color for the graph
nodeShapeStringboxA string specifying the shape of a node in the graph
nodeStyleStringroundedA string specifying the style of a node in the graph
nodeColorString#c6c5feDefault node color to use in the graph
noDependencyColorString#cfffacColor to use for nodes with no dependencies
cyclicNodeColorString#ff6c60Color to use for circular dependencies
edgeColorString#757575Edge color to use in the graph
graphVizOptionsObjectfalseCustom Graphviz options
graphVizPathStringnullCustom Graphviz path
detectiveOptionsObjectfalseCustom detective options for dependency-tree and precinct
dependencyFilterFunctionfalseFunction called with a dependency filepath (exclude substree by returning false)

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:

    "fontSize": "10px",
    "graphVizOptions": {
        "G": {
            "rankdir": "LR"



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.js path/src/app.js

Show modules that no one is depending on

$ madge --orphans path/src/

Excluding modules

$ madge --exclude '^(foo|bar)\.jsapos; path/src/app.js

Save graph as a SVG image (requires Graphviz)

$ madge --image graph.svg path/src/app.js

Save graph as a DOT file for further processing (requires Graphviz)

$ madge --dot path/src/app.js > graph.gv

Using pipe to transform tree (this example will uppercase all paths)

$ madge --json path/src/app.js | tr '[a-z]' '[A-Z]' | madge --stdin


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.

$ madge --debug path/src/app.js

Running tests

$ npm install
$ npm test


Missing dependencies?

It 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.

Using mixed import syntax in the same file?

Only one syntax is used by default. You can use both though if you're willing to take the degraded performance. Put this in your madge config to enable mixed imports.

For ES6 + CommonJS:

    "detectiveOptions": {
        "es6": {
            "mixedImports": true

For TypeScript + CommonJS:

    "detectiveOptions": {
        "ts": {
            "mixedImports": true

How to ignore import type statements in ES6 + Flow?

Put this in your madge config.

    "detectiveOptions": {
        "es6": {
            "skipTypeImports": true

How to ignore import in type annotations in TypeScript?

Put this in your madge config.

    "detectiveOptions": {
        "ts": {
            "skipTypeImports": true

What's the "Error: write EPIPE" when exporting graph to image?

Ensure you have installed Graphviz. 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.

How do I fix the "Graphviz not built with triangulation library" error when using sfdp layout?

Homebrew doesn't include GTS by default. Fix this by doing:

brew uninstall graphviz
brew install gts
brew install graphviz

The image produced by madge is very hard to read, what's wrong?

Try running madge with a different layout, here's a list of the ones you can try:

  • dot "hierarchical" or layered drawings of directed graphs. This is the default tool to use if edges have directionality.

  • 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 minimize a global energy function, which is equivalent to statistical multi-dimensional scaling.

  • fdp "spring model'' layouts similar to those of neato, but does this by reducing forces rather than working with energy.

  • sfdp multiscale version of fdp for the layout of large graphs.

  • twopi radial layouts, after Graham Wills 97. Nodes are placed on concentric circles depending their distance from a given root node.

  • 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.



This project exists thanks to all the people who contribute. Contributors


Thanks to the awesome people below for making donations! 🙏[Donate]

Landon Alder


Thank you so much all awesome backers! 🙏[Become a backer]


Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]


You can also support the project on Patreon. [Become a backer or sponsor]


MIT License

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free