packageDirOf
uses node:path
The directory of the package a file is in: the nearest with a package.json.
import fs from "node:fs"; import path from "node:path"; export function packageDirOf(file: string): string | undefined { for (let d = path.dirname(file); ; d = path.dirname(d)) { if (fs.existsSync(path.join(d, "package.json"))) return d; if (path.dirname(d) === d) return undefined; } }