LinuxCommandLibrary

yarn-why

Explain why a package is installed

TLDR

Show why a Yarn package is installed

$ yarn-why [package]
copy

SYNOPSIS

yarn why <package-name>

PARAMETERS

<package-name>
    The name of the package you want to investigate.

--all
    Show all paths that lead to the package. Useful for debugging conflicts.

--verbose
    Displays more verbose output.

--json
    Outputs the results in JSON format.

DESCRIPTION

The yarn why command is a powerful tool for understanding your project's dependency graph. It helps you trace back exactly *why* a particular package is included in your dependencies by showing the chain of dependencies that lead to it. This is invaluable for identifying accidental or unnecessary dependencies, understanding how transitive dependencies impact your project, and debugging dependency conflicts.

It works by traversing the dependency tree, starting from your project's direct dependencies (listed in your package.json file) and following their dependencies, and so on, until it finds the package you're investigating. The output then shows you the shortest path from a top-level dependency to the target package. This information is crucial for making informed decisions about managing your project's dependencies, improving performance, and reducing bundle sizes.

CAVEATS

The output may not always be perfectly clear, especially with complex dependency graphs. Understanding the package.json format and dependency resolution algorithms is helpful for interpreting the results.

EXAMPLE USAGE

To find out why your project depends on the package 'lodash', you would run:
yarn why lodash
The output will show the dependency chain, for example:
=> your-project@workspace:.
=> package-a@^1.0.0
=> lodash@^4.0.0

This output means that your project depends on 'package-a', which in turn depends on 'lodash'.

SEE ALSO

yarn add(1), yarn remove(1), yarn list(1)

Copied to clipboard