Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 28 days 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 9 years ago)
- Default Branch: main
- Last Pushed: 2023-11-03T20:02:50.000Z (about 1 year ago)
- Last Synced: 2024-04-14T09:52:32.507Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 28.3 KB
- Stars: 227
- Watchers: 7
- Forks: 24
- 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-cn - pkg-dir - 查找 npm 包的根目录 (包 / 文件系统)
- awesome-nodejs - pkg-dir - Find the root directory of an npm package. ![](https://img.shields.io/github/stars/sindresorhus/pkg-dir.svg?style=social&label=Star) (Repository / NPM)
- awesome-nodejs-cn - pkg-dir - **star:229** 查找npm包的根目录 (包 / 文件系统)
- awesome-nodejs - pkg-dir - Find the root directory of an npm package. (Packages / Filesystem)
- awesome-nodejs - pkg-dir - Find the root directory of a Node.js project or npm package - ★ 78 (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 - 查找 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