List Node Packages w/o Dependencies

npm list

npm list (or npm ls which is my preferred shorthand) is a useful way to determine what npm packages are currently installed. The issue is, you get all their dependencies too (and their dependencies dependencies and their dependencies dependencies dependencies…). A bit frustrating, since this list is usually exceptionally long and no amount of nesting formatting can fully combat this.

Added depth

Turns out there’s an easy way to combat this, although it’s not in the documentation for npm ls.

By adding --depth=0 to the end of the command, it only draws out the top of the dependency tree. Happy days:

$ npm ls --depth=0

Go global

This even works for globally installed packages too, by adding the standard -g flag.

I’m getting errors!

When I first tried this, I ended up with a bunch of nonsensical errors (e.g. “npm ERR! max depth reached” among others), although there was nothing really wrong.

You can combat this by adding "$@" 2>/dev/null to the end. This essentially ignores the pointless errors by redirecting them to /dev/null. Nothing to see here, move along :)

The complete command

$ npm ls --depth=0 "$@" 2>/dev/null

If you don’t want to type this out all the time (I certainly don’t), you can alias it up:

$ alias npmls="npm ls --depth=0 "$@" 2>/dev/null"

Then usage is as simple as:

$ npmls