Skip to content

findRoot

findRoot(startDir, marker, options?): Promise<string | undefined>

Defined in: find-root.ts:95

The nearest ancestor of startDir (itself first) that contains marker — a file or directory name, or a list of names where any one counts. For example ".git", "pnpm-workspace.yaml", or ["turbo.json", "nx.json"].

Each marker must be a bare name (no separators, ., .. or empty) or a TypeError is thrown; so is any filesystem error other than “not there”. Resolves to undefined when nothing is found, unless options.fallback is "start". See FindRootOptions; findRootSync is the synchronous twin. Built on findUpward.

string

string | readonly string[]

FindRootOptions = {}

Promise<string | undefined>

import { findRoot, findRootSync } from "@theholocron/fs-utils";
const repoRoot = await findRoot(process.cwd(), ".git");
const workspaceRoot = await findRoot(process.cwd(), ["pnpm-workspace.yaml", "turbo.json"]);
// never throws, never undefined: the git root, or the start dir when there is none
const root = findRootSync(cwd, ".git", { fallback: "start", realpath: true });