Skip to content

walkPaths

walkPaths(options?): AsyncGenerator<WalkedPath>

Defined in: packages/node/src/fs/walk-paths.ts:308

Streams the paths under a directory, filtered by globs, regular expressions or predicates.

Paths are yielded relative to the root, /-separated on every platform, and sorted within each directory. Because one traversal tests every filter, a path is yielded once no matter how many patterns it matches — no deduplication needed. Stop early by breaking out of the loop; the generator’s cleanup runs as usual.

Depth comes from the patterns, as it does in a shell: "*.ts" stays flat, "**/*.ts" recurses. WalkPathsOptions.descend overrides that, and is the only way to recurse with a regular expression or a predicate.

Time complexity: one readdir per directory descended into, and one filter test per entry visited. exclude is applied before descending, so an excluded subtree is never read.

WalkPathsOptions

Root, filters, depth, symlink and error handling. See WalkPathsOptions.

AsyncGenerator<WalkedPath>

The matching paths, each with the directory entry it came from.

Every .bench.ts outside node_modules ts for await (const { path } of walkPaths({ include: "**/*.bench.ts", exclude: "**/node_modules/**", })) { console.log(path); }

The CPU directories of sysfs, tolerating an absent tree ts const names = await Array.fromAsync( walkPaths({ root: "/sys/devices/system/cpu", include: /^cpu\d+$/, unreadable: "skip", }), (walked) => walked.path, );

When a directory cannot be read and WalkPathsOptions.unreadable does not tolerate it.