The dependency-tree CLI provides a command-line interface for analyzing dependency trees from the terminal. It supports both object tree and list output formats with integration for various build tool configurations.
Install globally to use the CLI:
npm install -g dependency-treeOr use with npx:
npx dependency-tree [options] <filename>dependency-tree [options] <filename>-d, --directory <path> Location of files of supported filetypes
-c, --require-config <path> Path to a RequireJS config file
-w, --webpack-config <path> Path to a Webpack config file
-t, --ts-config <path> Path to a TypeScript config file
--list-form Output the list form (one element per line)
-h, --help Display help information
-V, --version Output version numberdependency-tree -d ./src ./src/app.jsOutput (JSON format):
{
"/absolute/path/to/src/app.js": {
"/absolute/path/to/src/utils.js": {},
"/absolute/path/to/src/config.js": {
"/absolute/path/to/src/constants.js": {}
}
}
}dependency-tree --list-form -d ./src ./src/app.jsOutput (one file per line):
/absolute/path/to/src/utils.js
/absolute/path/to/src/constants.js
/absolute/path/to/src/config.js
/absolute/path/to/src/app.jsdependency-tree -d ./src -t ./tsconfig.json ./src/main.tsdependency-tree -d ./js -c ./require.config.js ./js/app.jsdependency-tree -d ./src -w ./webpack.config.js ./src/index.jsThe default output is a JSON representation of the dependency tree:
{
"/path/to/entry.js": {
"/path/to/dependency1.js": {
"/path/to/subdependency.js": {}
},
"/path/to/dependency2.js": {}
}
}With --list-form, outputs one file path per line in dependency order:
/path/to/subdependency.js
/path/to/dependency1.js
/path/to/dependency2.js
/path/to/entry.jsAdd to package.json scripts:
{
"scripts": {
"deps": "dependency-tree -d ./src ./src/index.js",
"deps-list": "dependency-tree --list-form -d ./src ./src/index.js",
"deps-ts": "dependency-tree -d ./src -t ./tsconfig.json ./src/main.ts"
}
}#!/bin/bash
# Get dependency list for bundling
DEPS=$(dependency-tree --list-form -d ./src ./src/app.js)
# Process each dependency
while IFS= read -r file; do
echo "Processing: $file"
# Add your processing logic here
done <<< "$DEPS"ENTRY_FILE = ./src/index.js
SRC_DIR = ./src
.PHONY: deps
deps:
dependency-tree -d $(SRC_DIR) $(ENTRY_FILE)
.PHONY: bundle
bundle:
dependency-tree --list-form -d $(SRC_DIR) $(ENTRY_FILE) | \
xargs cat > dist/bundle.js# Analyze with multiple configuration files
dependency-tree \
-d ./src \
-w ./webpack.config.js \
-t ./tsconfig.json \
./src/main.ts# Save tree to file
dependency-tree -d ./src ./src/app.js > dependency-tree.json
# Save list to file
dependency-tree --list-form -d ./src ./src/app.js > build-order.txt# Count total dependencies
dependency-tree --list-form -d ./src ./src/app.js | wc -l
# Find specific file types in dependencies
dependency-tree --list-form -d ./src ./src/app.js | grep '\.tsx\?$'
# Check for specific patterns
dependency-tree --list-form -d ./src ./src/app.js | grep node_modulesThe CLI provides helpful error messages for common issues:
$ dependency-tree
error: missing required argument 'filename'$ dependency-tree -d ./nonexistent ./src/app.js
# Outputs empty result for non-existent files$ dependency-tree -w ./invalid-webpack.config.js ./src/app.js
# Uses default resolution when config files are invalidThe CLI respects Node.js debugging environment variables:
# Enable debug output
DEBUG=tree dependency-tree -d ./src ./src/app.js
# Or use Node.js built-in debugging
NODE_DEBUG=tree dependency-tree -d ./src ./src/app.js-d option to the smallest relevant directory--list-form when you don't need the tree structureEmpty output: Check that the filename and directory paths are correct and accessible
Missing dependencies: Ensure all configuration files (webpack, tsconfig, etc.) are valid
Slow performance: Consider using more specific directory paths or adding filters via config files