find-root
recursively find the closest package.json
usage
Say you want to check if the directory name of a project matches its module name in package.json:
const path = const findRoot = // from a starting directory, recursively search for the nearest// directory containing package.jsonconst root = // => '/Users/jsdnxx/Code/find-root' const dirname = pathconsoleconsole
You can also pass in a custom check function (by default, it checks for the
existence of package.json
in a directory). In this example, we traverse up
to find the root of a git repo:
const fs = const gitRoot =
api
findRoot: (startingPath : string, check?: (dir: string) => boolean) => string
Returns the path for the nearest directory to startingPath
containing
a package.json
file, eg /foo/module
.
If check
is provided, returns the path for the closest parent directory
where check
returns true.
Throws an error if no package.json
is found at any level in the
startingPath
.
installation
> npm install find-root
running the tests
From package root:
> npm install> npm test
contributors
- jsdnxx
license
MIT. (c) 2017 jsdnxx