UNPKG

1.23 kBMarkdownView Raw
1# pkg-dir [![Build Status](https://travis-ci.org/sindresorhus/pkg-dir.svg?branch=master)](https://travis-ci.org/sindresorhus/pkg-dir)
2
3> Find the root directory of a Node.js project or npm package
4
5
6## Install
7
8```
9$ npm install --save pkg-dir
10```
11
12
13## Usage
14
15```
16/
17└── Users
18 └── sindresorhus
19 └── foo
20 ├── package.json
21 └── bar
22 ├── baz
23 └── example.js
24```
25
26```js
27// example.js
28const pkgDir = require('pkg-dir');
29
30pkgDir(__dirname).then(rootDir => {
31 console.log(rootDir);
32 //=> '/Users/sindresorhus/foo'
33});
34```
35
36
37## API
38
39### pkgDir([cwd])
40
41Returns a `Promise` for either the project root path or `null` if it couldn't be found.
42
43### pkgDir.sync([cwd])
44
45Returns the project root path or `null`.
46
47#### cwd
48
49Type: `string`<br>
50Default: `process.cwd()`
51
52Directory to start from.
53
54
55## Related
56
57- [pkg-dir-cli](https://github.com/sindresorhus/pkg-dir-cli) - CLI for this module
58- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
59- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories
60
61
62## License
63
64MIT © [Sindre Sorhus](https://sindresorhus.com)