Reliable method to find the root path of your git repository,
[p]npm/yarn package, or app.
The algorithm is primarily reliant on git rev-parse --show-toplevel,
but will fallback on the following glob patterns to determine the root path:
| Path/pattern | Examples |
|---|---|
pnpm-*.yaml |
pnpm-workspace.yaml, pnpm-lock.yaml |
package.{yaml,json} |
package.json, package.yaml |
package-lock.json |
|
npm-shrinkwrap.json |
|
yarn.lock |
|
.git* |
.gitignore, .gitattributes, .github/ |
.npmrc |
|
.editorconfig |
|
LICEN[SC]E* |
LICENSE.md |
.vscode/ |
|
packages/ |
|
src/ |
|
public/ |
import {root} from "@danielbayley/uproot"
console.log(root) // ~/path/to/project/rootimport {uproot} from "@danielbayley/uproot"
const cwd = import.meta.dirname
const root = await uproot({ cwd })You can override the above search patterns with the match option:
const root = await uproot({ match: ["tsconfig.json"] })or append an additional pattern to find:
import { uproot, match } from "@danielbayley/uproot"
match.push("tsconfig.json")
const root = await uproot({ match })Any additional options will be passed onto matchup:
import path from "node:path"
const object = await uproot({ cwd, parse: true })
const root = path.format(object)
console.log(root) // ~/path/to/project/rootpnpm install @danielbayley/matchupImportant
This package is ESM only, so must be imported instead of required,
and depends on Node.js >=20.
Specify this requirement with engines and/or devEngines: