You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB

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