# to-path-tree

> Convert a file path array to a tree.

Latest version **1.2.5** (published 2026-08-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install to-path-tree
pnpm add to-path-tree
yarn add to-path-tree
bun add to-path-tree
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.2.5 |
| Published | 2026-08-10 |
| First published | 2023-07-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 42.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | alexzhang1030 |
| Maintainers | alexzzz |
| Keywords | file path, path, tree, file paths to tree |

## Links

- npm: https://www.npmjs.com/package/to-path-tree
- Repository: https://github.com/alexzhang1030/to-path-tree
- npm.io page: https://npm.io/package/to-path-tree

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 1.2.5 (latest) — 2026-08-10
- 1.2.4 — 2025-10-11
- 1.2.3 — 2024-05-10
- 1.2.2 — 2024-05-10
- 1.2.1 — 2024-05-10
- 1.2.0 — 2024-05-10
- 1.1.8 — 2024-04-24
- 1.1.7 — 2024-04-17
- 1.1.6 — 2024-04-15
- 1.1.5 — 2024-04-15
- 1.1.4 — 2024-04-15
- 1.1.3 — 2024-04-15
- 1.1.2 — 2024-04-15
- 1.1.1 — 2024-04-15
- 1.1.0 — 2024-04-15
- … 10 more at https://npm.io/package/to-path-tree/versions

## README

# to-path-tree

<a href="https://www.npmjs.com/package/to-path-tree" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/npm/v/to-path-tree" alt="NPM Version" /></a>
<a href="https://www.npmjs.com/package/to-path-tree" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/npm/dt/to-path-tree" alt="NPM Downloads" /></a>
<a href="https://github.com/alexzhang1030/to-path-tree/blob/main/LICENSE" target="_blank" rel="noopener noreferrer"><img src="https://badgen.net/github/license/alexzhang1030/to-path-tree" alt="License" /></a>
<a href="https://codecov.io/gh/alexzhang1030/to-path-tree" ><img src="https://codecov.io/gh/alexzhang1030/to-path-tree/graph/badge.svg?token=NQ8S2Z38OB"/></a>

Convert a file path array to a tree.

## Installation

```bash
pnpm i to-path-tree
```

## Usage

### Function usage

```ts
import { pathToTree } from 'to-path-tree'

const paths = [
  'src/index.ts',
  'src/a/index.ts',
  'src/a/b/index.ts',
  'src/a/b/d/index.ts',
  'src/foo.ts',
  'src/b/index.js',
  'src/b/c/index.ts',
  'a/index.ts',
  'a/b.ts',
  'a/b/index.js'
]

const tree = pathToTree(paths)
```

### Builder usage

```ts
import { PathTreeBuilder } from 'to-path-tree'

const builder = new PathTreeBuilder()
builder.addPath('src/index.ts')
builder.addPath('src/a/index.ts')
builder.removePath('src/index.ts')
builder.getItems('src/a/') // get all file items under 'src/a/'
builder.getSubDirectories('src/') // get all subdirectories under 'src/'
```

### Tree types

The data structure of the tree is as follows:

```ts
export interface NodeItem<T> {
  path: string
  /**
   * e.g. index
   */
  filename: string
  /**
   * e.g. ts
   */
  ext: string
  /**
   * e.g. index.ts
   */
  file: string
  data?: T
  isEntry: boolean // the entry is index
  parent: TreeNode<T>
}

export interface TreeNode<T> {
  items: NodeItem<T>[]
  name: string
  /**
   * This path is strict path, must start with sep, e.g. `/`
   */
  path: string
  /**
   * Relative to parent path
   */
  relativePath: string
  /**
   * Relative to root path, exclude sep
   */
  relativePathName: string
  subDirectory: {
    [key: string]: TreeNode<T>
  } | null
  parent?: TreeNode<T>
}
```

### Traverse the tree

You can use `walkPathTree` to traverse the tree.

For example:

```ts
import { pathToTree, walkPathTree } from 'to-path-tree'

const tree = pathToTree(input)
walkPathTree(tree, (node) => {
  for (const item of node.items) {
    // NOTE: item.path is strict path, must start with sep, e.g. `/`
    if (item.path === '/src/a/b/d/index.ts') {
      // do something...
    }
  }
})
```

## License

MIT

---
_Source: https://npm.io/package/to-path-tree · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
