Overview
Thepm why command shows why a package is installed in your project by displaying the dependency chain that requires it.
Syntax
Arguments
string
required
The package name to investigate
string[]
Additional arguments passed to the underlying package manager
Examples
Check Why Package is Installed
Output
Check Transitive Dependency
Output
Check Deep Dependency
Output
Use Cases
Debug Unexpected Package
Find out why an unexpected package is installed:Understand Duplicates
Check why multiple versions exist:Audit Dependencies
Investigate security vulnerabilities:Plan Upgrades
Understand dependencies before upgrading:How It Works
Fromsrc/commands/why.ts:10-22:
- Detects your package manager
- Checks if it’s npm (not supported)
- Passes arguments to
pnpm whyorbun why - Displays the dependency tree
Package Manager Support
npm users can use
npm ls <package> as an alternative, though it provides different output format.Alternative for npm
If you’re using npm:Understanding Output
The output typically shows:- Package version - The installed version
- Dependency type - Whether it’s in dependencies, devDependencies, etc.
- Dependency chain - The tree of packages that require it
- Version ranges - The semver ranges that satisfied the requirement
Example Output Explained
- React 18.2.0 is installed
- It’s a direct dependency of your app
- react-dom requires react ^18
- ui-library also requires react ^18.0.0
Common Scenarios
Find Dependency Source
Debug Version Conflicts
Identify Bloat
Security Audit
Workflow Examples
Remove Unused Dependency
1
Check Usage
2
If No Direct Use
If it’s only a transitive dependency, consider alternatives
3
If Direct Dependency
Remove safely:
Investigate Duplicate
1
Check Why Installed
2
Review Version Ranges
Check if dependencies can use the same version
3
Update Dependencies
Align versions to remove duplicates: