CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-madge

Create graphs from module dependencies.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

cli.mddocs/

Command Line Interface

Full-featured CLI for dependency analysis from the command line. Includes options for output formatting, filtering, and visualization.

Installation

npm install -g madge

Capabilities

Basic Usage

madge [options] <src...>

The CLI accepts one or more source files or directories to analyze.

Usage Examples:

# Analyze single file
madge src/app.js

# Analyze multiple files
madge src/app.js src/utils.js

# Analyze directory
madge src/

# Analyze multiple directories
madge src/ lib/

Dependency Analysis Options

Options for controlling what dependencies to analyze and include.

-b, --basedir <path>        # Base directory for resolving paths
-x, --exclude <regexp>      # Exclude modules using RegExp
--extensions <list>         # Comma separated string of valid file extensions
--include-npm              # Include shallow NPM modules
--require-config <file>    # Path to RequireJS config
--webpack-config <file>    # Path to webpack config  
--ts-config <file>         # Path to typescript config

Usage Examples:

# Set base directory
madge --basedir /project/root src/

# Exclude node_modules
madge --exclude '^node_modules' src/

# Include specific file extensions
madge --extensions js,jsx,ts,tsx src/

# Include NPM dependencies
madge --include-npm src/

# Use TypeScript config
madge --ts-config tsconfig.json src/

Output Format Options

Options for controlling how results are displayed.

-j, --json                 # Output as JSON
-s, --summary             # Show dependency count summary
--no-color                # Disable color in output and image
--no-spinner              # Disable progress spinner
--warning                 # Show warnings about skipped files
--debug                   # Turn on debug output

Usage Examples:

# JSON output
madge --json src/

# Summary with counts
madge --summary src/

# Disable colors
madge --no-color src/

# Show warnings
madge --warning src/

Dependency Analysis Operations

Options for specific types of dependency analysis.

-c, --circular            # Show circular dependencies
-d, --depends <name>      # Show module dependents
--orphans                # Show modules that no one is depending on
--leaves                 # Show modules that have no dependencies
--no-count               # Disable circular dependencies counting

Usage Examples:

# Find circular dependencies
madge --circular src/

# Find what depends on a specific module
madge --depends utils.js src/

# Find orphaned modules
madge --orphans src/

# Find leaf modules (no dependencies)
madge --leaves src/

Graph Generation Options

Options for generating visual graphs and DOT output.

-i, --image <file>        # Write graph to file as an image
--dot                    # Show graph using the DOT language
-l, --layout <name>      # Layout engine to use for graph (dot/neato/fdp/sfdp/twopi/circo)
--rankdir <direction>    # Set the direction of the graph layout

Usage Examples:

# Generate SVG image
madge --image graph.svg src/

# Generate PNG image  
madge --image graph.png src/

# Generate DOT output
madge --dot src/ > graph.gv

# Use different layout engine
madge --layout neato --image graph.svg src/

# Change graph direction
madge --rankdir TB --image graph.svg src/

# Generate image with only circular dependencies
madge --circular --image circular.svg src/

Input Sources

The CLI supports multiple input methods.

--stdin                   # Read predefined tree from STDIN

Usage Example:

# Pipe dependency tree via STDIN
echo '{"a.js": ["b.js"], "b.js": []}' | madge --stdin --json

Configuration Files

Madge supports configuration via .madgerc or package.json.

.madgerc Configuration

{
  "fontSize": "10px",
  "graphVizOptions": {
    "G": {
      "rankdir": "LR"  
    }
  },
  "excludeRegExp": ["test", "spec"],
  "fileExtensions": ["js", "jsx", "ts", "tsx"]
}

package.json Configuration

{
  "name": "my-project",
  "madge": {
    "fontSize": "10px", 
    "backgroundColor": "#ffffff",
    "nodeColor": "#000000"
  }
}

Exit Codes

The CLI uses standard exit codes:

  • 0: Success, no circular dependencies found
  • 1: Circular dependencies found (when using --circular option)
  • 1: Error during execution

Output Examples

Default Output

┌────────────────────┐
│ Dependency graph   │
└────────────────────┘

src/app.js
  src/utils.js
  src/api.js

src/api.js  
  src/config.js

src/utils.js
  (no dependencies)

src/config.js
  (no dependencies)

✔ Processed 4 files (12ms)

JSON Output

madge --json src/
{
  "src/app.js": ["src/utils.js", "src/api.js"],
  "src/api.js": ["src/config.js"], 
  "src/utils.js": [],
  "src/config.js": []
}

Circular Dependencies Output

madge --circular src/
✖ Found 2 circular dependencies!

1) src/a.js > src/b.js > src/a.js
2) src/x.js > src/y.js > src/z.js > src/x.js

Summary Output

madge --summary src/
┌─────────────────────────────────────┐
│ Dependency Summary                  │
├─────────────────────────────────────┤
│ Total files        │ 12             │
│ Total dependencies │ 23             │
│ Circular dependencies │ 2           │
│ Orphaned files     │ 1              │
└─────────────────────────────────────┘

Requirements

  • Node.js: Version 18 or higher
  • Graphviz: Required for image generation (optional for other features)
    • Mac: brew install graphviz
    • Ubuntu: apt-get install graphviz

docs

api.md

cli.md

index.md

tile.json