dependency-tree

Get the dependency tree of a module

MIT 90 个版本
安装
npm install dependency-tree
yarn add dependency-tree
pnpm add dependency-tree
bun add dependency-tree
README

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

Option Type Default Description
filename string - Required. Absolute path to the entry file
directory string - Required. Root directory used to resolve relative paths
root string undefined Alias for directory
requireConfig string undefined Path to a RequireJS config for AMD modules (resolves aliased paths)
config string undefined Alias for requireConfig
webpackConfig string undefined Path to a webpack config for aliased modules
tsConfig string | object undefined Path to a TypeScript config file, or a preloaded config object
tsConfigPath string undefined Virtual path for the TypeScript config when tsConfig is an object. Required for Path Mapping; ignored when tsConfig is a string path
nodeModulesConfig object undefined Config for resolving node_modules entry files (e.g. { entry: 'module' })
visited object {} Memoization cache (filename to subtree) to skip already-processed files
nonExistent string[] [] Array populated with partial paths that could not be resolved
isListForm boolean false Return a flat post-order list of paths instead of a nested tree (same as calling dependencyTree.toList())
filter (dependencyPath: string, parentPath: string) => boolean undefined Return true to include a dependency (and its subtree) in the tree
detectiveConfig object {} Options passed to precinct for dependency extraction - e.g. { amd: { skipLazyLoaded: true } }, { ts: { skipTypeImports: true } }
detective object {} Alias for detectiveConfig
noTypeDefinitions boolean false Resolve 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
版本列表
12.0.1 2026-06-07
12.0.0 2026-05-19
11.5.0 2026-05-13
11.4.3 2026-04-20
11.4.2 2026-04-19
11.4.1 2026-04-19
11.4.0 2026-02-28
11.3.0 2026-02-09
11.2.0 2025-06-19
11.1.1 2025-02-07
11.1.0 2025-02-06
11.0.2 2025-02-01
11.0.1 2024-04-20
11.0.0 2024-04-14
10.0.9 2023-05-27
10.0.8 2023-05-25
10.0.7 2023-05-13
10.0.6 2023-05-12
10.0.5 2023-05-08
10.0.4 2023-05-07
10.0.3 2023-05-07
10.0.2 2023-05-07
10.0.1 2023-05-06
10.0.0 2023-05-05
9.0.0 2022-12-22
8.1.2 2021-10-08
8.1.1 2021-06-05
8.1.0 2021-04-17
8.0.0 2020-12-17
7.2.2 2020-11-01
7.2.1 2020-02-27
7.2.0 2019-11-29
7.1.0 2019-11-23
7.0.2 2019-01-17
7.0.1 2019-01-17
7.0.0 2019-01-05
6.5.0 2019-01-04
6.4.1 2019-01-04
6.4.0 2018-12-24
6.3.0 2018-11-19
6.2.1 2018-09-21
6.2.0 2018-09-21
6.1.1 2018-07-08
6.1.0 2018-04-14
6.0.1 2018-03-04
6.0.0 2018-01-19
5.12.0 2017-11-07
5.11.1 2017-09-30
5.11.0 2017-08-27
5.10.1 2017-08-26
5.10.0 2017-08-26
5.9.1 2017-04-19
5.9.0 2017-02-11
5.8.0 2017-02-04
5.7.6 2017-01-08
5.7.5 2017-01-04
5.7.4 2016-12-31
5.7.3 2016-12-31
5.7.2 2016-12-30
5.7.1 2016-10-03
5.7.0 2016-10-01
5.6.1 2016-09-08
5.6.0 2016-09-08
5.5.3 2016-09-03
5.5.2 2016-09-02
5.5.1 2016-08-31
5.5.0 2016-08-16
5.4.1 2016-07-14
5.4.0 2016-07-05
5.3.0 2016-06-19
5.2.0 2016-06-03
5.1.0 2016-05-11
5.0.0 2016-04-28
4.0.0 2015-10-01
3.1.1 2015-07-04
3.1.0 2015-06-29
3.0.0 2015-02-24
2.0.1 2014-12-24
2.0.0 2014-12-24
1.3.5 2014-12-24
1.3.4 2014-12-24
1.3.3 2014-11-26
1.3.2 2014-11-26
1.3.1 2014-11-25
1.3.0 2014-11-25
1.2.0 2014-11-24
1.1.1 2014-10-07
1.1.0 2014-07-18
1.0.0 2014-07-18
0.0.0 2014-07-18