Skip to content

Instantly share code, notes, and snippets.

@nicholascloud
Last active December 16, 2015 03:49
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholascloud/5372443 to your computer and use it in GitHub Desktop.
Save nicholascloud/5372443 to your computer and use it in GitHub Desktop.
blog-npm-root-packages

The problem

When I list my npm packages with npm ls -g (or without the -g option for a local node_modules directory) I see all installed packages and their dependencies. Like so:

$ npm ls -g
├─┬ anvil.js@0.9.0-RC3.1
│ ├── colors@0.6.0
│ ├─┬ commander@1.1.1
│ │ └── keypress@0.1.0
│ ├─┬ express@3.0.5
│ │ ├── buffer-crc32@0.1.1
│ │ ├── commander@0.6.1
│ │ ├─┬ connect@2.7.1
│ │ │ ├── bytes@0.1.0
│ │ │ ├── crc@0.2.0
│ │ │ ├── formidable@1.0.11
│ │ │ ├── pause@0.0.1
│ │ │ └── qs@0.5.1
│ │ ├── cookie@0.0.5
│ │ ├── cookie-signature@0.0.1
│ │ ├── debug@0.7.0
│ │ ├── fresh@0.1.0
│ │ ├── methods@0.0.1
│ │ ├── range-parser@0.0.4
│ │ └─┬ send@0.1.0
│ │   └── mime@1.2.6
│ ├─┬ fs-watch-tree@0.2.2
│ │ └── when@1.3.0
│ ├─┬ handlebars@1.0.7
│ │ ├─┬ optimist@0.3.5
│ │ │ └── wordwrap@0.0.2
│ │ └── uglify-js@1.2.6
│ ├── machina@0.2.2
│ ├─┬ minimatch@0.2.9
│ │ ├── lru-cache@2.0.4
│ │ └── sigmund@1.0.0
│ ├── mkdirp@0.3.3
│ ├── open@0.0.2
│ ├── postal@0.7.3
│ ├── should@0.6.3
│ ├─┬ socket.io@0.9.13
│ │ ├── base64id@0.1.0
│ │ ├── policyfile@0.0.4
│ │ ├── redis@0.7.3
│ │ └─┬ socket.io-client@0.9.11
│ │   ├─┬ active-x-obfuscator@0.0.1
│ │   │ └── zeparser@0.0.5
│ │   ├── uglify-js@1.2.5
│ │   ├─┬ ws@0.4.25
│ │   │ ├── commander@0.6.1
│ │   │ ├── options@0.0.3
│ │   │ └── tinycolor@0.0.1
│ │   └── xmlhttprequest@1.4.2
│ └── underscore@1.4.3
├─┬ grunt@0.3.17
│ ├── async@0.1.22
│ ├── colors@0.6.0-1
│ ├─┬ connect@2.4.6
│ │ ├── bytes@0.1.0
│ │ ├── cookie@0.0.4
│ │ ├── crc@0.2.0
│ │ ├── debug@0.7.0
│ │ ├── formidable@1.0.11
│ │ ├── fresh@0.1.0
│ │ ├── pause@0.0.1
│ │ ├── qs@0.5.1
│ │ └─┬ send@0.0.4
│ │   ├── mime@1.2.6
│ │   └── range-parser@0.0.4
│ ├── dateformat@1.0.2-1.2.3
│ ├─┬ glob-whatev@0.1.8
│ │ └─┬ minimatch@0.2.9
│ │   ├── lru-cache@2.0.4
│ │   └── sigmund@1.0.0
│ ├─┬ gzip-js@0.3.1
│ │ ├── crc32@0.2.2
│ │ └── deflate-js@0.2.2
│ ├── hooker@0.2.3
│ ├─┬ jshint@0.9.1
│ │ ├─┬ cli@0.4.3
│ │ │ └─┬ glob@3.1.14
│ │ │   ├── graceful-fs@1.1.14
│ │ │   ├── inherits@1.0.0
│ │ │   └─┬ minimatch@0.2.9
│ │ │     ├── lru-cache@2.0.4
│ │ │     └── sigmund@1.0.0
│ │ └─┬ minimatch@0.0.5
│ │   └── lru-cache@1.0.6
│ ├─┬ nodeunit@0.7.4
│ │ └─┬ tap@0.3.2
│ │   ├── buffer-equal@0.0.0
│ │   ├── deep-equal@0.0.0
│ │   ├─┬ difflet@0.2.3
│ │   │ ├── charm@0.0.8
│ │   │ └── traverse@0.6.3
│ │   ├── inherits@1.0.0
│ │   ├── mkdirp@0.3.4
│ │   ├─┬ nopt@2.0.0
│ │   │ └── abbrev@1.0.3
│ │   ├─┬ runforcover@0.0.2
│ │   │ └─┬ bunker@0.1.2
│ │   │   └─┬ burrito@0.2.12
│ │   │     ├── traverse@0.5.2
│ │   │     └── uglify-js@1.1.1
│ │   ├── slide@1.1.3
│ │   └── yamlish@0.0.5
│ ├─┬ nopt@1.0.10
│ │ └── abbrev@1.0.3
│ ├─┬ prompt@0.1.12
│ │ ├── pkginfo@0.2.3
│ │ └─┬ winston@0.5.11
│ │   ├── eyes@0.1.8
│ │   ├─┬ loggly@0.3.11
│ │   │ ├── request@2.9.203
│ │   │ └── timespan@2.2.0
│ │   └── stack-trace@0.0.6
│ ├── semver@1.0.14
│ ├─┬ temporary@0.0.5
│ │ └── package@1.0.1
│ ├── uglify-js@1.3.4
│ ├── underscore@1.2.4
│ └── underscore.string@2.1.1
├─┬ jake@0.5.8
│ ├─┬ minimatch@0.2.9
│ │ ├── lru-cache@2.0.4
│ │ └── sigmund@1.0.0
│ └── utilities@0.0.19
├─┬ jitsu@0.11.4
│ ├── colors@0.6.0-1
│ ├── complete@0.3.1
│ ├── dateformat@1.0.2-1.2.3
│ ├─┬ flatiron@0.3.0
│ │ ├─┬ broadway@0.2.5
│ │ │ ├─┬ cliff@0.1.8
│ │ │ │ └── eyes@0.1.8
│ │ │ ├── eventemitter2@0.4.9
│ │ │ ├─┬ nconf@0.6.4
│ │ │ │ ├── async@0.1.22
│ │ │ │ └── ini@1.0.5
│ │ │ ├─┬ utile@0.1.2
│ │ │ │ ├── async@0.1.22
│ │ │ │ ├── deep-equal@0.0.0
│ │ │ │ ├── i@0.3.1
│ │ │ │ ├── mkdirp@0.3.4
│ │ │ │ ├── ncp@0.2.6
│ │ │ │ └── rimraf@1.0.9
│ │ │ └─┬ winston@0.6.2
│ │ │   ├── async@0.1.22
│ │ │   ├── cycle@1.0.1
│ │ │   ├── eyes@0.1.8
│ │ │   └── stack-trace@0.0.6
│ │ ├── director@1.1.6
│ │ ├─┬ optimist@0.3.4
│ │ │ └── wordwrap@0.0.2
│ │ └─┬ prompt@0.2.8
│ │   ├─┬ read@1.0.4
│ │   │ └── mute-stream@0.0.3
│ │   ├── revalidator@0.1.5
│ │   ├─┬ utile@0.1.6
│ │   │ ├── async@0.1.22
│ │   │ ├── deep-equal@0.0.0
│ │   │ ├── i@0.3.1
│ │   │ ├── mkdirp@0.3.4
│ │   │ ├── ncp@0.2.6
│ │   │ └── rimraf@1.0.9
│ │   └─┬ winston@0.6.2
│ │     ├── async@0.1.22
│ │     ├── cycle@1.0.1
│ │     ├── eyes@0.1.8
│ │     └── stack-trace@0.0.6
│ ├── flatiron-cli-config@0.1.3
│ ├── flatiron-cli-users@0.1.1
│ ├─┬ fstream@0.1.18
│ │ ├── graceful-fs@1.1.14
│ │ ├── inherits@1.0.0
│ │ ├── mkdirp@0.3.4
│ │ └── rimraf@2.1.1
│ ├─┬ fstream-npm@0.1.1
│ │ ├─┬ fstream-ignore@0.0.5
│ │ │ └─┬ minimatch@0.2.9
│ │ │   ├── lru-cache@2.0.4
│ │ │   └── sigmund@1.0.0
│ │ └── inherits@1.0.0
│ ├── ladder@0.0.0
│ ├── nodejitsu-api@0.3.6
│ ├─┬ npm@1.1.59
│ │ ├── abbrev@1.0.3
│ │ ├── ansi@0.1.2
│ │ ├── archy@0.0.2
│ │ ├── block-stream@0.0.6
│ │ ├── chownr@0.0.1
│ │ ├── couch-login@0.1.10
│ │ ├── fstream@0.1.18
│ │ ├─┬ fstream-npm@0.1.1
│ │ │ └── fstream-ignore@0.0.5
│ │ ├── glob@3.1.12
│ │ ├── graceful-fs@1.1.10
│ │ ├── inherits@1.0.0
│ │ ├── ini@1.0.4
│ │ ├─┬ init-package-json@0.0.5
│ │ │ └── promzard@0.2.0
│ │ ├── lockfile@0.2.1
│ │ ├── lru-cache@2.0.1
│ │ ├── minimatch@0.2.6
│ │ ├── mkdirp@0.3.4
│ │ ├── node-gyp@0.6.7
│ │ ├── nopt@2.0.0
│ │ ├── npm-registry-client@0.2.6
│ │ ├─┬ npmconf@0.0.11
│ │ │ └─┬ config-chain@1.1.1
│ │ │   └── proto-list@1.2.2
│ │ ├── npmlog@0.0.2
│ │ ├── once@1.1.1
│ │ ├── osenv@0.0.3
│ │ ├─┬ read@1.0.4
│ │ │ └── mute-stream@0.0.3
│ │ ├── read-installed@0.0.2
│ │ ├── read-package-json@0.1.4
│ │ ├── request@2.9.203
│ │ ├── retry@0.6.0
│ │ ├── rimraf@2.0.2
│ │ ├── semver@1.0.14
│ │ ├── slide@1.1.3
│ │ ├── tar@0.1.13
│ │ ├── uid-number@0.0.3
│ │ └── which@1.0.5
│ ├── opener@1.3.0
│ ├── pkginfo@0.2.3
│ ├── progress@0.1.0
│ ├── request@2.9.203
│ ├─┬ require-analyzer@0.4.0-3
│ │ ├─┬ detective@0.0.4
│ │ │ └─┬ burrito@0.2.12
│ │ │   ├── traverse@0.5.2
│ │ │   └── uglify-js@1.1.1
│ │ ├── eyes@0.1.8
│ │ ├─┬ findit@0.0.5
│ │ │ └─┬ seq@0.3.5
│ │ │   ├─┬ chainsaw@0.0.9
│ │ │   │ └── traverse@0.3.9
│ │ │   └─┬ hashish@0.0.4
│ │ │     └── traverse@0.6.3
│ │ ├─┬ npm@1.1.4
│ │ │ ├── abbrev@1.0.3
│ │ │ ├── block-stream@0.0.5
│ │ │ ├── fast-list@1.0.2
│ │ │ ├── fstream@0.1.12
│ │ │ ├── graceful-fs@1.1.5
│ │ │ ├── inherits@1.0.0
│ │ │ ├── ini@1.0.2
│ │ │ ├── lru-cache@1.0.5
│ │ │ ├── minimatch@0.1.5
│ │ │ ├── mkdirp@0.3.0
│ │ │ ├── node-uuid@1.3.3
│ │ │ ├── nopt@1.0.10
│ │ │ ├── proto-list@1.0.0
│ │ │ ├── read@0.0.1
│ │ │ ├── request@2.9.153
│ │ │ ├── rimraf@2.0.1
│ │ │ ├── semver@1.0.13
│ │ │ ├── slide@1.1.3
│ │ │ ├── tar@0.1.12
│ │ │ └── which@1.0.5
│ │ ├─┬ optimist@0.2.8
│ │ │ └── wordwrap@0.0.2
│ │ └─┬ winston@0.5.11
│ │   ├── async@0.1.22
│ │   ├─┬ loggly@0.3.11
│ │   │ └── timespan@2.2.0
│ │   └── stack-trace@0.0.6
│ ├── semver@1.0.14
│ ├── spawn-command@0.0.2
│ ├─┬ tar@0.1.13
│ │ ├── block-stream@0.0.6
│ │ └── inherits@1.0.0
│ └─┬ wizard@0.0.1
│   └─┬ prompt@0.2.9
│     ├─┬ read@1.0.4
│     │ └── mute-stream@0.0.3
│     ├── revalidator@0.1.5
│     ├─┬ utile@0.1.6
│     │ ├── async@0.1.22
│     │ ├── deep-equal@0.0.0
│     │ ├── i@0.3.1
│     │ ├── mkdirp@0.3.4
│     │ ├── ncp@0.2.6
│     │ └── rimraf@1.0.9
│     └─┬ winston@0.6.2
│       ├── async@0.1.22
│       ├── cycle@1.0.1
│       ├── eyes@0.1.8
│       └── stack-trace@0.0.6
├─┬ jshint@0.9.1
│ ├─┬ cli@0.4.3
│ │ └─┬ glob@3.1.14
│ │   ├── graceful-fs@1.1.14
│ │   ├── inherits@1.0.0
│ │   └─┬ minimatch@0.2.9
│ │     ├── lru-cache@2.0.4
│ │     └── sigmund@1.0.0
│ └─┬ minimatch@0.0.5
│   └── lru-cache@1.0.6
├─┬ npm@1.1.65
│ ├── abbrev@1.0.3
│ ├── ansi@0.1.2
│ ├── archy@0.0.2
│ ├── block-stream@0.0.6
│ ├── chownr@0.0.1
│ ├── fstream@0.1.19
│ ├─┬ fstream-npm@0.1.3
│ │ └── fstream-ignore@0.0.5
│ ├── glob@3.1.14
│ ├── graceful-fs@1.1.14
│ ├── inherits@1.0.0
│ ├── ini@1.0.5
│ ├─┬ init-package-json@0.0.6
│ │ └── promzard@0.2.0
│ ├── lockfile@0.2.1
│ ├── lru-cache@2.0.4
│ ├── minimatch@0.2.8
│ ├── mkdirp@0.3.4
│ ├── node-gyp@0.7.1
│ ├── nopt@2.0.0
│ ├─┬ npm-registry-client@0.2.10
│ │ └── couch-login@0.1.15
│ ├─┬ npmconf@0.0.16
│ │ └─┬ config-chain@1.1.2
│ │   └── proto-list@1.2.2
│ ├── npmlog@0.0.2
│ ├── once@1.1.1
│ ├── opener@1.3.0
│ ├── osenv@0.0.3
│ ├─┬ read@1.0.4
│ │ └── mute-stream@0.0.3
│ ├── read-installed@0.0.3
│ ├── read-package-json@0.1.8
│ ├── request@2.9.203
│ ├── retry@0.6.0
│ ├── rimraf@2.0.2
│ ├── semver@1.1.0
│ ├── slide@1.1.3
│ ├── tar@0.1.13
│ ├── uid-number@0.0.3
│ └── which@1.0.5
├─┬ phantomjs@0.2.2
│ ├─┬ rimraf@2.0.2
│ │ └── graceful-fs@1.1.14
│ └─┬ unzip@0.0.4
│   ├─┬ binary@0.3.0
│   │ ├── buffers@0.1.1
│   │ └─┬ chainsaw@0.1.0
│   │   └── traverse@0.3.9
│   ├─┬ fstream@0.1.19
│   │ ├── graceful-fs@1.1.14
│   │ ├── inherits@1.0.0
│   │ └── mkdirp@0.3.4
│   └─┬ pullstream@0.0.4
│     ├── over@0.0.5
│     └── stream-buffers@0.2.3
└─┬ stylus@0.30.1
  ├── cssom@0.2.5
  ├── debug@0.7.0
  └── mkdirp@0.3.4

Ugly, yes? I am typically not interested in the dependencies, only top-level packages. I searched the npm man page for a way to filter this ungainly output but alas, I was denied. Or too dumb to see what was right in front of me. In either case, I was left with my problem.

The solution

I added the following snippet to my ~/.bash_profile file:

function npmls() {
  npm ls "$@" | grep ^[└├]
}

To make the change effective, I sourced the file:

$ source ~/.bash_profile

Now, when I execute the npmls function (to which I can pass standard npm arguments), it will filter the output to show only top-level packages.

$ npmls -g
├─┬ anvil.js@0.9.0-RC3.1
├─┬ grunt@0.3.17
├─┬ jake@0.5.8
├─┬ jitsu@0.11.4
├─┬ jshint@0.9.1
├─┬ npm@1.1.65
├─┬ phantomjs@0.2.2
└─┬ stylus@0.30.1

Ah, much better. I can put the eye bleach back under the sink now.

@darrencauthon
Copy link

I hope there is no such thing as eye bleach.

@nicholascloud
Copy link
Author

@darrentcauthon new from Apple: iBleach

@giodamelio
Copy link

Revised version that make the grep work with ZSH and changes the ─┬'s to ──'s.

function npmls() {
    npm ls "$@" | grep "^[└├]" | sed "s/─┬/──/g"
}

@qjcg
Copy link

qjcg commented Apr 26, 2013

I use the built-in --depth flag (no need to resort to grep or sed) :

function npmls() {
  npm ls --depth=0 "$@" 2>/dev/null
}

Errors are redirected to /dev/null to avoid superfluous messages like these that show up:

npm ERR! max depth reached: jade@0.30.0, required by undefined@undefined
npm ERR! max depth reached: js-yaml@2.0.4, required by undefined@undefined
...

@nicholascloud
Copy link
Author

@qjcg Awesome, I did not know about the depth flag! Thanks!

@rayshan
Copy link

rayshan commented Jul 5, 2013

Thanks @qjcg, I searched for the max depth error and there isn't much help elsewhere besides your comment.

@kodybrown
Copy link

Excellent! Just what I was looking for! Thanks!

For Windows, I couldn't get grep (yes, the built-in Windows version) to work with the fancy trees. So I did this:

> npm config set unicode=false

Now, I created a batch file that looks like this:

> type npmls.bat
@npm ls -g %* | grep "^+--"

You could also use a macro if you wanted - basically the same thing as an alias in *nix):

> doskey npmls=npm ls ^| grep "^+--"

---- UPDATED ----

Then... I finished reading this thread.. :-P ..

Thanks to @qjcg, I can use the depth argument instead, which I like a lot better. Here is the Windows equivalent:

> npm ls --depth=0 2>NUL

The new batch file:

> type npmls.bat
@npm ls --depth=0 %* 2>NUL

Or, the new macro:

> doskey npmls=npm ls --depth=0 2^>NUL

This also allows me to put the nice looking trees back in.. (npm config set unicode=true)

Thanks guys!

@samtsai
Copy link

samtsai commented Feb 28, 2014

Anyone else having issues with the depth flag recently? I feel like with one of the latest updates, 'depth' doesn't do anything...

@matthewrankin
Copy link

@samtsai Yes, the depth doesn't appear to be working in npm v1.4.3 and v1.4.4. As a workaround, you could install npmlist.

@tmugford
Copy link

tmugford commented May 7, 2014

Apparently the --depth issue was fixed as of v1.4.6.. I've updated to v1.4.10 and it's definitely working again for me. So, to recap, you can get a list of globally installed packages minus their dependencies using npm list -g --depth 0.

@bojoer
Copy link

bojoer commented Jun 6, 2014

The correct wa yto get the global installed npm packages without their dependencies is now:

npm list -g --depth=0

@bburns
Copy link

bburns commented Jul 28, 2014

I was on npm 1.4.3 on Windows, tried npm update -g npm, which didn't do anything - it's apparently another issue (see npm/npm#1840).

So just install the latest version of node, which includes npm - mine is up to 1.4.14 now, and --depth works.

Also, you can set 0 as the default with npm set depth 0 (can add -g to store it as your global setting), then just do npm list -g. You can override it with npm list -g --depth 1, etc.

(and both --depth 1 and --depth=1 seem to work alright for this version)

@cweekly
Copy link

cweekly commented Sep 4, 2014

Thanks @tmugford! :)

@gnrlbzik
Copy link

thanks @qjcg

@scott-joe
Copy link

npm list -g --depth 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment