recursively find the closest package.json
Say you want to check if the directory name of a project matches its module name in package.json:
var path = require('path')
var findRoot = require('find-root')
// from a starting directory, recursively search for the nearest
// directory containing package.json
var root = findRoot('/Users/jden/dev/find-root/tests')
// => '/Users/jden/dev/find-root'
var dirname = path.basename(root)
console.log('is it the same?')
console.log(dirname === require(path.join(root, 'package.json')).name)
Returns the path for the nearest directory to startingPath
containing
a package.json
file, eg /foo/module
.
Throws an error if no package.json
is found at any level in the
startingPath
.
If packageName
is passed, it will only return if the name
property in
the package.json
file is matched. Otherwise it continues up the file tree.
$ npm install find-root
From package root:
$ npm install
$ npm test
- jden [email protected]
MIT. (c) MMXIII AgileMD http://agilemd.com