npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

dtre

v2.0.0

Published

Programmatically print a simple directory tree in plain text. It's just an easy way for me to get the format I like.

Downloads

269

Readme

dtre: Directory Tree

/ˈditriː/

Programmatically print a simple directory tree in plain text or JSON. It's just an easy way for me to get the format I like.

Contents

  1. Features
  2. Module
  3. Command-line Interface (CLI)
    1. Configuration files
      1. Local configuration files
      2. Global configuration files

Features

  • Recursive Tree Generation: Prints directory structure recursively.
  • Exclusion Support: Exclude files or folders using regex patterns.
  • Custom Styles: Supports default and backtick formatting styles.
  • Output Options:
    • Prints raw text output to --stdout
    • Writes to a file
    • JSON
    • Overview JSON Output: Includes file content and metadata.
  • Configuration Files:
    • Supports local (dtre.json) and
    • global (~/.config/dtre.json) configuration.
  • CLI Options: Override configuration with command-line arguments.

Module exports a function printDirectoryTree(dir, style, outputPath, excludes, jsonOutput = false, overview = false) where:

  • dir is the directory for which you want the tree.
  • style is the branch formatting style (e.g., 'default' or 'backtick').
  • outputPath is the file path for saving output, or null to print to the console.
  • excludes is a regex pattern string for files/folders to exclude, or null for no exclusions.
  • jsonOutput (optional) outputs the directory tree in JSON format if true.
  • overview (optional) includes file metadata and content in JSON output if true.

Example:

import { printDirectoryTree } from "./index.js";

printDirectoryTree('test', 'backtick', 'dist/directory_tree.json', null, true, true);

This generates a JSON file ./dist/directory_tree.json with content like:

{
    "name": "test",
    "type": "directory",
    "children": [
        {
            "name": "a1.txt",
            "type": "file",
            "extension": "txt",
            "content": "Sample content of a1.txt"
        }
    ]
}

Import the global npm package:

$ npm install --global dtre

Command-line interface supports various options:

# Print the tree for a directory
$ dtre --directoryPath="assets"
$ dtre -d assets

# Specify the style of output
$ dtre --style="default"
$ dtre -s backtick

# Write the output to a file
$ dtre --outputPath="dir.txt"
$ dtre -o dir.txt

# Exclude files or folders matching a regex
$ dtre --excludes="^[_\.]"
$ dtre -e "^[_\.]"

# Use a global configuration file
$ dtre --global
$ dtre -g

# Output JSON with file content and metadata (overview mode)
$ dtre --overview
$ dtre -w

Place a dtre.json file in the current working directory. Example:

{
    "directoryPath": "./src",
    "style": "backtick",
    "outputPath": "./tree-output.txt",
    "excludes": "node_modules",
    "jsonOutput": false,
    "overview": false
}

Create a global configuration file in ~/.config/dtre.json. Example:

{
    "directoryPath": "./",
    "style": "default",
    "outputPath": null,
    "excludes": null,
    "jsonOutput": false,
    "overview": false
}

Important: To use the global configuration, run with the --global or -g flag.

LICENSE

MIT © Sam Liebl 2024.