npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

dependency-tree

v12.0.0

Published

Get the dependency tree of a module

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