How does npm draw the dependency tree?

npm does a nifty job of drawing a package's dependency hierarchy as a tree in the console:

$ npm ls
my-awesome-project@0.0.1
├── colors@0.6.0-1
├─┬ express@2.5.11
│ ├─┬ connect@1.9.2
│ │ └── formidable@1.0.11
│ ├── mime@1.2.4
│ ├── mkdirp@0.3.0
│ └── qs@0.4.2
└── uglify-js@1.2.6

How does npm do this?

npm uses the Unicode box drawing characters (U+2500-2800) to draw the pretty lines of the tree.

To draw a similar tree in your own application, the best route is probably to use the same module that npm itself uses – archy.

var archy = require('archy');
var s = archy({
  label : 'beep',
  nodes : [
    'ity',
    {
      label : 'boop',
      nodes : [
        {
          label : 'o_O',
          nodes : [
            {
              label : 'oh',
              nodes : [ 'hello', 'puny' ]
            },
            'human'
          ]
        },
        'party\ntime!'
      ]
    }
  ]
});
console.log(s);

Outputs

beep
├── ity
└─┬ boop
  ├─┬ o_O
  │ ├─┬ oh
  │ │ ├── hello
  │ │ └── puny
  │ └── human
  └── party
      time!