Count packages installed in a pnpm or aube workspace

Use a recursive, parseable listing and count paths containing node_modules or the licences command.

There is no built-in command to get the number of installed dependencies. The install command shows it though. But sometimes you just want the number without a hassle.

pnpm

This works in pnpm:

pnpm ls -r --parseable --depth=Infinity | grep -c '/node_modules/'
  • --recursive visits workspace packages.
  • --parseable prints paths instead of a tree.
  • --depth=Infinity includes transitive dependencies.

The count is a count of installed package paths, so it can include multiple copies of a package when different versions are present.

But it doesn't work with aube, because aube’s output differs from pnpm.

aube

But, perhaps more conveniently, you can query the licences to get the same number:

aube licenses --json | jq length

That also works in pnpm.

This number is not exactly the same as what aube install shows. This returns a deduplicated resolved-package inventory excluding workspace packages. aube install or pnpm install shows an install-operation count, influenced by platform-specific optional packages, which the licenses doesn't cover. But it gets you a useful number you can work with if you're looking to optimize the dependency count.