AboutΒ Β β’Β Β Getting StartedΒ Β β’Β Β RecipesΒ Β β’Β Β CLI Reference
Dependency analysis and optimization toolkit for modern TypeScript projects.
Completely rewritten in Go for maximum speed and efficiency β‘
The tool was created to help with daily development struggles by answering these questions:
π What entry points does my codebase have?
π Which entry points use a given file?
π Which dependencies does a given file have?
π Do any circular dependencies exist in the project?
π Which node modules are unused or not listed in package.json?
π Which node modules take up the most space on disk?
This helps to debug project dependencies, plan refactoring, optimize bundles or plan code splitting.
It's especially useful in JS world without TypeScript or tests coverage.
It also helps to identify and eliminate dead files, understand the complexity of the file dependencies
- You plan to refactor some file and you wonder which entry points are affected
- You are wondering wether a given source file is used
- You wonder if there are any dead files in your project
- You want to verify if a given entry point imports only the required files
- You want to optimize the amount of files imported by an entry point
- You want to detect circular dependencies in your project
- You want to find unused node modules to clean up dependencies
- You want to identify which node modules are consuming the most disk space
There are tool that can output nice, visual representation of project dependencies like webpack-bundle-analyzer or dependency-cruiser (which btw rev-dep uses for non-TS codebases)
While graphs can be useful to identify major problems like too big bundle size or to visualize mess in your deps, it's hard to take any action based on them (at least it was hard for me π€·ββοΈ)
rev-dep visualize dependencies as lists, so it's really easy to see where to cut the line to solve the problem.
yarn global add rev-dep
npm -g install rev-dep
pnpm global add rev-dep
Just use rev-dep resolve --file path/to/file.ts
You will see all the entry points that implicitly require given file together with resolution path.
To find out all paths combination use rev-dep resolve with -a flag
You might be surprised how complex dependency tree can be!
Use rev-dep resolve --file path/to/file.ts --compact-summary
As a result you will see total amount of entry points requiring a given file.
Note that among the entry points list there might be some dead files importing the searched file
Use rev-dep entry-points to get list of all files that are not required by any other files in the project.
You might want to exclude some file paths that are meant to be actual entry point like index.js or **/pages/** in next.js projects using --result-exclude flag. The same for configuration files like babel.config.js
Review the list and look for suspicious files like src/ui/components/SomeComponent/index.js
entry-points command CLI reference
To get a full list of files imported by given entry point use rev-dep files --entry-point path/to/file.ts.
You can use --count flag if you are interested in the amount.
This is a good indicator of how heavy a given entry point or component is
There is no easy how to for this process, but you can do it iteratively using rev-dep commands files and resolve
-
Get the list of files imported by entry-point
rev-dep files --entry-point path/to/entry-point -
Identify some suspicious files on the list, components that should not be used on the given page or not related utility files
-
Get all resolution paths for a suspicious file
rev-dep resolve --file path/to/suspicious-file --entry-points path/to/entry-point --all -
You would usually find out that there is some file, like directory
indexfile that given entry point is using, which is mandatory, but as a side effect it imports a few files that are redundant for your entry point. In most cases you should be able to decouple the imports or reverse the dependency to cut off the resolution path for the unwanted file
Use rev-dep circular to find all circular dependencies between modules in your project.
Circular dependencies can cause runtime errors, memory leaks, and make code difficult to understand and maintain. It's important to identify and resolve them early.
You can use --ignore-type-imports flag to exclude type-only imports from the analysis if they're not relevant to your use case.
circular command CLI reference
Use rev-dep node-modules unused to identify packages that are installed but not actually imported in your code.
This helps clean up your package.json and reduce the size of your node_modules directory. You might want to exclude type definitions using --exclude-modules=@types/* since they're often used indirectly.
node-modules unused command CLI reference
Use rev-dep node-modules dirs-size to calculate and display the size of node_modules directories.
This helps identify which packages are taking up the most disk space, allowing you to make informed decisions about dependency management or look for lighter alternatives.
For detailed analysis of specific modules, use rev-dep node-modules analyze-size with the package names you want to investigate.
node-modules dirs-size command CLI reference
Rev-dep@2.0.0 was reimplemented in Go from scratch to leverage it's concurrency features and better memory management of compiled languages.
As a result v2 is up to 37x faster than v1 and consumes up to 13x less memory.
To compare performance rev-dep was benchmarked with hyperfine using 8 runs per test, taking mean time values as a result. Benchmark was run on TypeScript codebase with 507658 lines of code and 5977 source code files.
Memory usage on Mac was measure using /usr/bin/time utility. Memory usage on Linux was not measured because I could't find reliable way to measure RAM usage on Linux. Subsequent runs had too much fluctuation.
| Command | V1 Time | V2 Time | Time Change | V1 RAM | V2 RAM | RAM Change |
|---|---|---|---|---|---|---|
List entry-points rev-dep entry-points |
6500ms | 347ms | 19x | ~680MB | ~51MB | 13x |
List entry-points with dependent files count -pdc |
8333ms | 782ms | 11x | ~885MB | ~110MB | 8x |
List entry-point files rev-dep files |
2729ms | 400ms | 7x | ~330MB | ~36MB | 9x |
Resolve dependency path rev-dep resolve |
2984ms | 359ms | 8x | ~330MB | ~35MB | 9x |
| Command | V1 Time | V2 Time | Time Change |
|---|---|---|---|
List entry-points rev-dep entry-points |
9904ms | 270ms | 37x |
List entry-points with dependent files count --print-deps-count |
10562ms | 458ms | 23x |
List entry-point files rev-dep files |
3097ms | 230ms | 13x |
Resolve dependency path rev-dep resolve |
3146ms | 230ms | 14x |
V2 comes with bunch of new commands
circular- detects circular dependencies in the projectlines-of-code- counts actual lines of code in the project excluding comments and blank lineslist-cwd-files- lists all files in the current working directorynode-modules used- lists all used node modulesnode-modules unused- lists all unused node modulesnode-modules missing- lists all missing node modulesnode-modules installed- lists all installed node modulesnode-modules installed-duplicates- lists all installed node modules that exist in file system with the same version multiple timesnode-modules analyze-size- analyzes size of specific node modules and helps to identify space-hogging dependenciesnode-modules dirs-size- calculates cumulative files size in node_modules directories
Comparing to previous versions, these tsconfig features are not supported
If you tsconfig uses extends, and you have some paths defined in config being extended, paths from extended config won't be used during resolution
eg.
// tsconfig.json
{
"extends": "./tsconfig.base.json",
"paths": {
"@/components": ["src/components/*"]
}
}// tsconfig.base.json
{
"paths": {
"@/utils": ["src/utils/*"]
}
}In that scenario imports with @/utils won't be resolved.
Why it's not supported ? I consider this typescript capability as an usage edge-case. I forgot to to implement it at the begging and I don't feel like investing more time now, before this package get some reasonable adoption and people will be actually requesting this feature.
Only first path will be used in resolution.
// tsconfig.json
{
"paths": {
"@/components": ["src/components/*", "src/components2/*"]
}
}Imports that should resolve to src/components2/* will be considered unresolved.
Why it's not supported? I consider this typescript capability as an anti-pattern. It introduces unnecessary ambiguity in module resolution. Implementing this would make code more complex, less maintainable and slower.
Importing rev-dep in JS/TS is no longer supported. Preferred way is to run rev-dep using child process.
Any other discrepancies between TypeScript module resolution and rev-dep should be considered as a bug.
- Linux x64
- MacOS Apple Silicon
There are the platforms I use and know. For these I build and tested binaries. Go allows for cross-compiling, so I'm happy to build and distribute binaries for other platforms as well, but I haven't tested it. Feel free to open an issue if you need support for another platform.
Detect circular dependencies in your project
Analyzes the project to find circular dependencies between modules. Circular dependencies can cause hard-to-debug issues and should generally be avoided.
rev-dep circular [flags]
rev-dep circular --ignore-types-imports
-c, --cwd string Working directory for the command (default "$PWD")
-h, --help help for circular
-t, --ignore-type-imports Exclude type imports from the analysis
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
Discover and list all entry points in the project
Analyzes the project structure to identify all potential entry points. Useful for understanding your application's architecture and dependencies.
rev-dep entry-points [flags]
rev-dep entry-points --print-deps-count
-n, --count Only display the number of entry points found
-c, --cwd string Working directory for the command (default "$PWD")
--graph-exclude strings Exclude files matching these glob patterns from analysis
-h, --help help for entry-points
-t, --ignore-type-imports Exclude type imports from the analysis
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--print-deps-count Show the number of dependencies for each entry point
--result-exclude strings Exclude files matching these glob patterns from results
--result-include strings Only include files matching these glob patterns in results
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
List all files in the dependency tree of an entry point
Recursively finds and lists all files that are required by the specified entry point.
rev-dep files [flags]
rev-dep files --entry-point src/index.ts
-n, --count Only display the count of files in the dependency tree
-c, --cwd string Working directory for the command (default "$PWD")
-p, --entry-point string Entry point file to analyze (required)
-h, --help help for files
-t, --ignore-type-imports Exclude type imports from the analysis
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
Count actual lines of code in the project excluding comments and blank lines
rev-dep lines-of-code [flags]
rev-dep lines-of-code
-c, --cwd string Directory to analyze (default "$PWD")
-h, --help help for lines-of-code
List all files in the current working directory
Recursively lists all files in the specified directory, with options to filter results.
rev-dep list-cwd-files [flags]
rev-dep list-cwd-files --include='*.ts' --exclude='*.test.ts'
--count Only display the count of matching files
--cwd string Directory to list files from (default "$PWD")
--exclude strings Exclude files matching these glob patterns
-h, --help help for list-cwd-files
--include strings Only include files matching these glob patterns
Analyze and manage Node.js dependencies
Tools for analyzing and managing Node.js module dependencies. Helps identify unused, missing, or duplicate dependencies in your project.
rev-dep node-modules used -p src/index.ts
rev-dep node-modules unused --exclude-modules=@types/*
rev-dep node-modules missing --entry-points=src/main.ts
-h, --help help for node-modules
Calculates cumulative files size in node_modules directories
Calculates and displays the size of node_modules folders in the current directory and subdirectories. Sizes will be smaller than actual file size taken on disk. Tool is calculating actual file size rather than file size on disk (related to disk blocks usage)
rev-dep node-modules dirs-size [flags]
rev-dep node-modules dirs-size
-c, --cwd string Working directory for the command (default "$PWD")
-h, --help help for dirs-size
Find and optimize duplicate package installations
Identifies packages that are installed multiple times in node_modules. Can optimize storage by creating symlinks between duplicate packages.
rev-dep node-modules installed-duplicates [flags]
rev-dep node-modules installed-duplicates --optimize --size-stats
-c, --cwd string Working directory for the command (default "$PWD")
-h, --help help for installed-duplicates
--isolate Create symlinks only within the same top-level node_module directories. By default optimize creates symlinks between top-level node_module directories (eg. when workspaces are used). Needs --optimize flag to take effect
--optimize Automatically create symlinks to deduplicate packages
--size-stats Print node modules dirs size before and after optimization. Might take longer than optimization itself
--verbose Show detailed information about each optimization
List all installed npm packages in the project
Recursively scans node_modules directories to list all installed packages. Helpful for auditing dependencies across monorepos.
rev-dep node-modules installed [flags]
rev-dep node-modules installed --include-modules=@myorg/*
-c, --cwd string Working directory for the command (default "$PWD")
-e, --exclude-modules strings list of modules to exclude from the output
-h, --help help for installed
-i, --include-modules strings list of modules to include in the output
Find imported packages not listed in package.json
Identifies packages that are imported in your code but not declared in your package.json dependencies.
rev-dep node-modules missing [flags]
rev-dep node-modules missing --entry-points=src/main.ts
-n, --count Only display the count of modules
-c, --cwd string Working directory for the command (default "$PWD")
-p, --entry-points strings Entry point file(s) to start analysis from (default: auto-detected)
-e, --exclude-modules strings list of modules to exclude from the output
-b, --files-with-binaries strings Additional files to search for binary usages. Use paths relative to cwd
-m, --files-with-node-modules strings Additional files to search for module imports. Use paths relative to cwd
--group-by-file Organize output by project file path
--group-by-module Organize output by npm package name
-h, --help help for missing
-t, --ignore-type-imports Exclude type imports from the analysis
-i, --include-modules strings list of modules to include in the output
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--pkg-fields-with-binaries strings Additional package.json fields to check for binary usages
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
--zero-exit-code Use this flag to always return zero exit code
Find installed packages that aren't imported in your code
Compares package.json dependencies with actual imports in your codebase to identify potentially unused packages.
rev-dep node-modules unused [flags]
rev-dep node-modules unused --exclude-modules=@types/*
-n, --count Only display the count of modules
-c, --cwd string Working directory for the command (default "$PWD")
-p, --entry-points strings Entry point file(s) to start analysis from (default: auto-detected)
-e, --exclude-modules strings list of modules to exclude from the output
-b, --files-with-binaries strings Additional files to search for binary usages. Use paths relative to cwd
-m, --files-with-node-modules strings Additional files to search for module imports. Use paths relative to cwd
-h, --help help for unused
-t, --ignore-type-imports Exclude type imports from the analysis
-i, --include-modules strings list of modules to include in the output
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--pkg-fields-with-binaries strings Additional package.json fields to check for binary usages
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
--zero-exit-code Use this flag to always return zero exit code
List all npm packages imported in your code
Analyzes your code to identify which npm packages are actually being used. Helps keep track of your project's runtime dependencies.
rev-dep node-modules used [flags]
rev-dep node-modules used -p src/index.ts --group-by-module
-n, --count Only display the count of modules
-c, --cwd string Working directory for the command (default "$PWD")
-p, --entry-points strings Entry point file(s) to start analysis from (default: auto-detected)
-e, --exclude-modules strings list of modules to exclude from the output
-b, --files-with-binaries strings Additional files to search for binary usages. Use paths relative to cwd
-m, --files-with-node-modules strings Additional files to search for module imports. Use paths relative to cwd
--group-by-file Organize output by project file path
--group-by-module Organize output by npm package name
-h, --help help for used
-t, --ignore-type-imports Exclude type imports from the analysis
-i, --include-modules strings list of modules to include in the output
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--pkg-fields-with-binaries strings Additional package.json fields to check for binary usages
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
Trace and display the dependency path between files in your project
Analyze and display the dependency chain between specified files. Helps understand how different parts of your codebase are connected.
rev-dep resolve [flags]
rev-dep resolve -p src/index.ts -f src/utils/helpers.ts
-a, --all Show all possible resolution paths, not just the first one
--compact-summary Display a compact summary of found paths
-c, --cwd string Working directory for the command (default "$PWD")
-p, --entry-points strings Entry point file(s) to start analysis from (default: auto-detected)
-f, --file string Target file to check for dependencies
--graph-exclude strings Glob patterns to exclude files from dependency analysis
-h, --help help for resolve
-t, --ignore-type-imports Exclude type imports from the analysis
--package-json string Path to package.json (default: ./package.json) (default "package.json")
--tsconfig-json string Path to tsconfig.json (default: ./tsconfig.json) (default "tsconfig.json")
Made in π΅π± and π―π΅ with π§ by @jayu
I hope that this small piece of software will help you discover and understood complexity of your project hence make you more confident while refactoring. If this tool was useful, don't hesitate to give it a β!
