Back to Trending
RepositoryJavaScriptUSUnited States
DE

dependents/node-dependency-tree

Get the dependency tree of a module

778stars
82forks
11issues
0views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: dependency-tree

#1 Repository of the month

2
TINIX TRENDING·Topic: commonjs

#2 Repository of the month

2
TINIX TRENDING·Topic: stylus

#2 Repository of the month

dependency-tree

CI npm version npm downloads

Get the dependency tree of a module

npm install dependency-tree
  • Supports JS (AMD, CommonJS, ES6), TypeScript, and CSS preprocessors (PostCSS, Sass, Stylus, Less) - any type handled by precinct
    • CommonJS: third-party (npm) dependencies are included by default
    • Path resolution is handled by filing-cabinet; RequireJS and webpack loaders are supported
  • Core Node built-ins (assert, path, fs, etc.) are excluded by default

Usage

// ESM
import dependencyTree from 'dependency-tree';
// CommonJS
const { default: dependencyTree } = require('dependency-tree');

// Returns a nested dependency tree object for the given file
const tree = dependencyTree({
  filename: 'path/to/a/file',
  directory: 'path/to/all/files',
  requireConfig: 'path/to/requirejs/config', // optional
  webpackConfig: 'path/to/webpack/config', // optional
  tsConfig: 'path/to/typescript/config', // optional
  nodeModulesConfig: {
    entry: 'module'
  }, // optional
  filter: (dependencyPath, parentPath) => !dependencyPath.includes('node_modules'), // optional
  nonExistent: [], // optional
  noTypeDefinitions: false // optional
});

// Returns a post-order flat list of absolute paths (dependencies before dependents).
// Useful as a concatenation order for bundling.
const list = dependencyTree.toList({
  filename: 'path/to/a/file',
  directory: 'path/to/all/files'
});

Options

OptionTypeDefaultDescription
filenamestring-Required. Absolute path to the entry file
directorystring-Required. Root directory used to resolve relative paths
rootstringundefinedAlias for directory
requireConfigstringundefinedPath to a RequireJS config for AMD modules (resolves aliased paths)
configstringundefinedAlias for requireConfig
webpackConfigstringundefinedPath to a webpack config for aliased modules
tsConfigstring | objectundefinedPath to a TypeScript config file, or a preloaded config object
tsConfigPathstringundefinedVirtual path for the TypeScript config when tsConfig is an object. Required for Path Mapping; ignored when tsConfig is a string path
nodeModulesConfigobjectundefinedConfig for resolving node_modules entry files (e.g. { entry: 'module' })
visitedobject{}Memoization cache (filename to subtree) to skip already-processed files
nonExistentstring[][]Array populated with partial paths that could not be resolved
isListFormbooleanfalseReturn a flat post-order list of paths instead of a nested tree (same as calling dependencyTree.toList())
filter(dependencyPath: string, parentPath: string) => booleanundefinedReturn true to include a dependency (and its subtree) in the tree
detectiveConfigobject{}Options passed to precinct for dependency extraction - e.g. { amd: { skipLazyLoaded: true } }, { ts: { skipTypeImports: true } }
detectiveobject{}Alias for detectiveConfig
noTypeDefinitionsbooleanfalseResolve TypeScript imports to *.js instead of *.d.ts

Output format

The default output is a nested object where every key is an absolute file path and the value is its own subtree:

{
  '/path/to/a.js': {
    '/path/to/b.js': {
      '/path/to/d.js': {},
      '/path/to/e.js': {}
    },
    '/path/to/c.js': {
      '/path/to/f.js': {},
      '/path/to/g.js': {}
    }
  }
}

This format was designed for visual representation in the Dependents plugin.

CLI

Requires a global install: npm install -g dependency-tree

dependency-tree --directory=path/to/files [--list-form] [--es6-mixed-imports] [-c path/to/require/config] [-w path/to/webpack/config] filename

Prints the dependency tree as JSON. Use --list-form to print one path per line instead.

How it works

dependency-tree passes the entry file to precinct to extract raw dependency strings, then passes each to filing-cabinet to resolve them to real filesystem paths, and recurses until the full tree is built.

Precinct generates an AST via node-source-walk, detects the module format (CommonJS, AMD, or ES6), and delegates to the appropriate detective to extract dependency declarations.

Filing-cabinet reuses that AST to detect the module format again, then delegates to the right resolver - necessary because AMD supports path aliasing via a RequireJS config, while CommonJS has its own resolution algorithm. The resolver returns an absolute path, which dependency-tree then recurses into.

FAQ

Why aren't some dependencies being detected?

Bugs in precinct or an incomplete requireConfig/webpackConfig/tsConfig are the most common causes. Any path that could not be resolved is appended to the array passed as nonExistent.

For detailed resolution logs, set NODE_DEBUG=* when using the CLI:

NODE_DEBUG=* dependency-tree -w path/to/webpack.config.json path/to/a/file

Similar Projects

Discover other similar projects and AI models.

RepositoryJavaScript
pahen/madge

Create graphs from your CommonJS, AMD or ES6 module dependencies

10.2k
Jan 21, 2026
Repository
getify/You-Dont-Know-JS

A book series (2 published editions) on the JS language.

184.8k
Oct 31, 2026
RepositoryJavaScript
twbs/bootstrap

The most popular HTML, CSS, and JavaScript framework for developing responsive, mobile first projects on the web.

175.1k
Dec 1, 2026
RepositoryJavaScript
airbnb/javascript

JavaScript Style Guide

148.0k
Apr 16, 2026
RepositoryJavaScript
react/react

The library for web and native user interfaces.

253.0k
Nov 30, 2026
RepositoryJavaScript
trekhleb/javascript-algorithms

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

196.8k
Jul 26, 2026
RepositoryTypeScript
morluto/rea

Reverse engineer anything with agents, from app behavior down to native binaries.

487.0k
Dec 2, 2026
RepositoryTypeScript
freeCodeCamp/freeCodeCamp

freeCodeCamp.org's open-source codebase and curriculum. Learn math, programming, and computer science for free.

459.9k
Dec 2, 2026
RepositoryPython
practical-tutorials/project-based-learning

Curated list of project-based tutorials

290.9k
Nov 30, 2026