UNPKG

538 BJavaScriptView Raw
1var path = require('path')
2var fs = require('fs')
3
4function findRoot(start) {
5 start = start || module.parent.filename
6 if (typeof start === 'string') {
7 if (start[start.length-1] !== path.sep) {
8 start+=path.sep
9 }
10 start = start.split(path.sep)
11 }
12 if(!start.length) {
13 throw new Error('package.json not found in path')
14 }
15 start.pop()
16 var dir = start.join(path.sep)
17 try {
18 fs.statSync(path.join(dir, 'package.json'));
19 return dir;
20 } catch (e) {}
21 return findRoot(start)
22}
23
24module.exports = findRoot