Petros Kyriakoupersonal blog

I am a full stack web developer. Love tinkering all the time, especially anything javascript related.

How to generate a directory tree

December 24, 2019


If you were in search on how to create a directory tree structure dynamically like those beautiful directory trees you see in some github repositories then look no further.

While I was doing my own research on the subject I came upon this very easy-to-use library that can do it for you.

Introducing dree

As the name might suggest, dree is a nodejs module that helps generate a directory as an object or as a string representation.

Usually we are interested in the string representation and that is what I am going to showcase here.


# suggested global install unless you want to use the object representation or custom configuration
npm install -g dree


# Get a string representation
# Command
dree parse <source> --dest <outputDirectory> --name <filename>
# Usage
dree parse ./ --dest ./ --name result

What the above command will do is to scan current directory and save the output in result.txt in same directory.


The output in the results.txt file will be something like the following:

 ├── .prettierrc
 ├─> .vscode
 │   └── settings.json
 ├── gatsby-browser.js
 ├── gatsby-config.js
 ├── gatsby-node.js
 ├── gatsby-ssr.js
 ├── package.json
 ├── result.txt
 ├─> src
 │   ├── blog-config.js
 │   ├─> components

Advanced usage

In the output above, I deliberately excluded some directories that I did not want to display like the node_modules folder or the .git directory.

To do that you add an --exclude flag to the command like so:

dree parse ./ --dest ./ --name result --exclude .git node_modules


This is a really handy way to generate dynamically a directory tree and the dree can do so much more if you take a look at its documentation.

Until next time!