https://github.com/sindresorhus/pkg-dir
  
  
    Find the root directory of a Node.js project or npm package 
    https://github.com/sindresorhus/pkg-dir
  
        Last synced: 6 months ago 
        JSON representation
    
Find the root directory of a Node.js project or npm package
- Host: GitHub
- URL: https://github.com/sindresorhus/pkg-dir
- Owner: sindresorhus
- License: mit
- Created: 2015-09-01T07:39:43.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2023-11-03T20:02:50.000Z (almost 2 years ago)
- Last Synced: 2025-03-31T02:11:37.742Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 240
- Watchers: 6
- Forks: 20
- Open Issues: 2
- 
            Metadata Files:
            - Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
 
Awesome Lists containing this project
- awesome-nodejs - pkg-dir - Find the root directory of an npm package.  (Repository / NPM)
- awesome-nodejs - pkg-dir - Find the root directory of an npm package. (Packages / Filesystem)
- awesome-node - pkg-dir - Find the root directory of an npm package. (Packages / Filesystem)
- awesome-nodejs-cn - pkg-dir - 查找npm包的根目录. (目录 / 文件系统)
- awesome-nodejs - pkg-dir - Find the root directory of a Node.js project or npm package - ★ 78 (Filesystem)
- awesome-nodejs-cn - pkg-dir - 查找 npm 包的根目录 (包 / 文件系统)
- awesome-nodejs-cn - pkg-dir - **star:240** 查找npm包的根目录 (包 / 文件系统)
- fucking-awesome-nodejs - pkg-dir - Find the root directory of an npm package. (Packages / Filesystem)
- fucking-awesome-nodejs - pkg-dir - Find the root directory of an npm package. (Packages / Filesystem)
- awesome-nodejs - pkg-dir - 查找 npm 或 Node.js 包的根目录 (Uncategorized / Uncategorized)
README
          # pkg-dir
> Find the root directory of a Node.js project or npm package
## Install
```sh
npm install pkg-dir
```
## Usage
```
/
└── Users
    └── sindresorhus
        └── foo
            ├── package.json
            └── bar
                ├── baz
                └── example.js
```
```js
// example.js
import {packageDirectory} from 'pkg-dir';
console.log(await packageDirectory());
//=> '/Users/sindresorhus/foo'
```
## API
### packageDirectory(option?)
Returns a `Promise` for either the project root path or `undefined` if it could not be found.
### packageDirectorySync(options?)
Returns the project root path or `undefined` if it could not be found.
#### options
Type: `object`
##### cwd
Type: `string`\
Default: `process.cwd()`
The directory to start searching from.
## Related
- [pkg-dir-cli](https://github.com/sindresorhus/pkg-dir-cli) - CLI for this module
- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
- [find-up](https://github.com/sindresorhus/find-up) - Find a file by walking up parent directories