or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

cli.mdconfiguration.mdindex.mdlist-generation.mdtree-analysis.md
tile.json

tessl/npm-dependency-tree

Get the dependency tree of a module across JavaScript, TypeScript, and CSS preprocessor formats

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/dependency-tree@11.2.x

To install, run

npx @tessl/cli install tessl/npm-dependency-tree@11.2.0

index.mddocs/

Dependency Tree

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.

Package Information

  • Package Name: dependency-tree
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install dependency-tree

Core Imports

const dependencyTree = require('dependency-tree');

For ES6 modules:

import dependencyTree from 'dependency-tree';

TypeScript:

import dependencyTree, { Options, Tree } from 'dependency-tree';

Basic Usage

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);

Architecture

Dependency Tree is built around several key components:

  • Main Analysis Engine: Core dependency traversal with cycle detection and memoization
  • Configuration System: Robust option validation and normalization for various build tools
  • Module Resolution: Integration with filing-cabinet for resolving module paths across different formats
  • AST Processing: Uses precinct for extracting dependencies from source code via AST parsing
  • CLI Interface: Command-line tool for dependency analysis workflows

Capabilities

Tree Analysis

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;

Tree Analysis

List Generation

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[];

List Generation

Command Line Interface

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 tree

Command Line Interface

Configuration Management

Advanced configuration system supporting RequireJS, Webpack, TypeScript, and Node.js module resolution with extensive customization options.

class Config {
  constructor(options: Options);
  clone(): Config;
}

Configuration

Supported File Types

  • JavaScript: AMD, CommonJS, ES6 modules
  • TypeScript: Full TypeScript support with path mapping
  • CSS Preprocessors: Sass (.scss, .sass), Stylus (.styl), Less (.less)
  • CSS: PostCSS and standard CSS imports

Error Handling

The library gracefully handles common error scenarios:

  • Missing Files: Non-existent files are tracked in nonExistent array
  • Circular Dependencies: Automatic cycle detection prevents infinite loops
  • Invalid Configurations: Clear error messages for configuration issues
  • Module Resolution Failures: Graceful fallback with detailed logging