Search code examples
javascriptnode.jsnpmyarnpkgpnpm

List all dependents of a dependency from lock file


Is it possible to list all dependents of specific dependency from a lock file (npm/pnpm/yarn)?

  • It's the opposite of yarn ls
  • I prefer not to read the lock file if there is a built-in command in npm/pnpm/yarn

I know that I can ask npm registery for that but I'm looking for a solution without network calls (expect npm/pnpm/yarn install)


Solution

  • pnpm and Yarn both have a why command. So you can run <yarn|pnpm> why <pkg>.

    In the case of pnpm, pnpm why <pkg> does the same as pnpm ls <pkg> --depth Infinity. It is because pnpm creates a non-flat node_modules.

    Output example of pnpm why glob:

    @pnpm/registry-mock 1.8.0
    ├─┬ cpr 3.0.1
    │ └─┬ rimraf 2.7.1
    │   └── glob 7.1.5
    ├─┬ rimraf 3.0.0
    │ └── glob 7.1.5
    └─┬ verdaccio 4.3.4
      ├─┬ bunyan 1.8.12
      │ └─┬ mv 2.1.1
      │   └─┬ rimraf 2.4.5
      │     └── glob 6.0.4
      └─┬ mv 2.1.1
        └─┬ rimraf 2.4.5
          └── glob 6.0.4
    tslint 5.20.0
    └── glob 7.1.5
    verdaccio 4.3.4
    ├─┬ bunyan 1.8.12
    │ └─┬ mv 2.1.1
    │   └─┬ rimraf 2.4.5
    │     └── glob 6.0.4
    └─┬ mv 2.1.1
      └─┬ rimraf 2.4.5
        └── glob 6.0.4