UNPKG

1.55 kBMarkdownView Raw
1# pkg-dir
2
3> Find the root directory of a Node.js project or npm package
4
5## Install
6
7```sh
8npm install pkg-dir
9```
10
11## Usage
12
13```
14/
15└── Users
16 └── sindresorhus
17 └── foo
18 ├── package.json
19 └── bar
20 ├── baz
21 └── example.js
22```
23
24```js
25// example.js
26import {packageDirectory} from 'pkg-dir';
27
28console.log(await packageDirectory());
29//=> '/Users/sindresorhus/foo'
30```
31
32## API
33
34### packageDirectory(option?)
35
36Returns a `Promise` for either the project root path or `undefined` if it could not be found.
37
38### packageDirectorySync(options?)
39
40Returns the project root path or `undefined` if it could not be found.
41
42#### options
43
44Type: `object`
45
46##### cwd
47
48Type: `string`\
49Default: `process.cwd()`
50
51The directory to start searching from.
52
53## Related
54
55- [pkg-dir-cli](https://github.com/sindresorhus/pkg-dir-cli) - CLI for this module
56- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
57- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories
58
59---
60
61<div align="center">
62 <b>
63 <a href="https://tidelift.com/subscription/pkg/npm-pkg-dir?utm_source=npm-pkg-dir&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
64 </b>
65 <br>
66 <sub>
67 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
68 </sub>
69</div>