8

I need to view a dependency tree of some sort, showing the various require()s starting at a particular file. For example, if I have a server.js file like so:

// server.js
var myThing = require('./myThing');

and a myThing.js file like so:

// myThings.js
var mongodb = require('mongodb');

is there a way to see that mongodb is required by server.js without manually traversing through myThing.js ?

I'd love to see a tree something like npm list generates, eg:

alex@alex-pc ~/repos/test $ npm list
test@1.0.0 /home/alex/repos/test
├─┬ gulp@3.8.11
│ ├── archy@1.0.0
│ ├─┬ chalk@0.5.1
│ │ ├── ansi-styles@1.1.0
│ │ ├── escape-string-regexp@1.0.3
│ │ ├─┬ has-ansi@0.1.0
│ │ │ └── ansi-regex@0.2.1
│ │ ├─┬ strip-ansi@0.3.0
│ │ │ └── ansi-regex@0.2.1
│ │ └── supports-color@0.2.0
│ ├── deprecated@0.0.1
Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88

3 Answers3

8

You can see the dependency tree using madge

gilamran
  • 7,090
  • 4
  • 31
  • 50
1

dependo

It is not sort, but visualize your files

sarkiroka
  • 1,485
  • 20
  • 28
0

I don't know any existing packages for this but you can create one using module.parent and module.children properties. Read here. To get packages' versions you should read package.json of every package.

Also npm itself can contain useful code. You can try to get it here - https://github.com/npm/npm

zag2art
  • 4,869
  • 1
  • 29
  • 39
  • How will that work with `require('./myThing')` which is just a single `myThing.js` file - not an entire package with a `package.json` ? – Alex McMillan May 30 '15 at 17:07
  • The `myThing.js` will be in `server.js`'s module.children, but without any `package.json`. So, by this fact you can detect it's not a package. – zag2art May 30 '15 at 17:19