Dependency Tree is a comprehensive dependency analysis library that extracts and maps the complete dependency structure of JavaScript, TypeScript, and CSS preprocessor modules. It provides both object-form tree representation and linear list traversal with support for multiple module formats (AMD, CommonJS, ES6 modules) and build tool configurations.
npm install dependency-treeconst dependencyTree = require('dependency-tree');For ES6 modules:
import dependencyTree from 'dependency-tree';TypeScript:
import dependencyTree, { Options, Tree } from 'dependency-tree';const dependencyTree = require('dependency-tree');
// Get dependency tree as object
const tree = dependencyTree({
filename: './src/app.js',
directory: './src'
});
// Get dependencies as a list for bundling
const list = dependencyTree.toList({
filename: './src/app.js',
directory: './src'
});
console.log('Tree:', tree);
console.log('List:', list);Dependency Tree is built around several key components:
Primary dependency tree extraction functionality that recursively analyzes modules and returns comprehensive dependency maps. Supports circular dependency detection and configurable filtering.
function dependencyTree(options: Options): Tree;
interface Options {
filename: string;
directory: string;
visited?: Tree;
nonExistent?: string[];
isListForm?: boolean;
requireConfig?: string;
webpackConfig?: string;
nodeModulesConfig?: any;
detectiveConfig?: any;
tsConfig?: string | Record<string, any>;
noTypeDefinitions?: boolean;
filter?: (path: string, parent: string) => boolean;
}
interface TreeInnerNode {
[parent: string]: TreeInnerNode | string;
}
type Tree = TreeInnerNode | string;Post-order traversal functionality that returns dependencies as a flat array in proper concatenation order for bundling. Eliminates duplicate subtrees for optimal bundling performance.
function toList(options: Options): string[];CLI tool for dependency analysis with output formatting options and integration with build tool configurations.
dependency-tree [options] <filename>
Options:
-d, --directory <path> location of files of supported filetypes
-c, --require-config <path> path to a requirejs config
-w, --webpack-config <path> path to a webpack config
-t, --ts-config <path> path to a typescript config
--list-form output the list form of the treeAdvanced configuration system supporting RequireJS, Webpack, TypeScript, and Node.js module resolution with extensive customization options.
class Config {
constructor(options: Options);
clone(): Config;
}The library gracefully handles common error scenarios:
nonExistent array