# find-pkg-dir

> Find the root directory of a Node.js project from a given path

Latest version **2.0.0** (published 2019-05-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install find-pkg-dir
pnpm add find-pkg-dir
yarn add find-pkg-dir
bun add find-pkg-dir
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2019-05-31 |
| First published | 2018-05-21 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/find-pkg-dir) |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 5.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Shinnosuke Watanabe |
| Maintainers | shinnn |
| Keywords | find, resolve, search, package, package.json, recursive, root, directory, lookup |

## Links

- npm: https://www.npmjs.com/package/find-pkg-dir
- Repository: https://github.com/shinnn/find-pkg-dir
- Homepage: https://github.com/shinnn/find-pkg-dir#readme
- Issues: https://github.com/shinnn/find-pkg-dir/issues
- npm.io page: https://npm.io/package/find-pkg-dir

## Dependencies (1)

- [inspect-with-kind](https://npm.io/package/inspect-with-kind.md) ^1.0.5

## Alternatives

- [base64url](https://npm.io/package/base64url.md) — 6.1M weekly downloads
- [get-installed-path](https://npm.io/package/get-installed-path.md) — 502.9K weekly downloads
- [@uppy/url](https://npm.io/package/@uppy/url.md) — 185.8K weekly downloads
- [@d3fc/d3fc-shape](https://npm.io/package/@d3fc/d3fc-shape.md) — 16.2K weekly downloads
- [localizer](https://npm.io/package/localizer.md) — 226 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2019-05-31
- 1.0.1 — 2018-07-16
- 1.0.0 — 2018-05-31
- 0.2.1 — 2018-05-26
- 0.2.0 — 2018-05-24
- 0.1.0 — 2018-05-22
- 0.0.0 — 2018-05-21

## README

# find-pkg-dir

[![npm version](https://img.shields.io/npm/v/find-pkg-dir.svg)](https://www.npmjs.com/package/find-pkg-dir)
[![Build Status](https://travis-ci.com/shinnn/find-pkg-dir.svg?branch=master)](https://travis-ci.com/shinnn/find-pkg-dir)
[![codecov](https://codecov.io/gh/shinnn/find-pkg-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/find-pkg-dir)

Find the root directory of a Node.js project from a given path

```javascript
const findPkgDir = require('find-pkg-dir');

// When the /Users/shinnn/foo directory contains a package.json file:

findPkgDir('/Users/shinnn/foo'); //=> '/Users/shinnn/foo'
findPkgDir('/Users/shinnn/foo/bar'); //=> '/Users/shinnn/foo'
findPkgDir('/Users/shinnn/foo/bar/baz'); //=> '/Users/shinnn/foo'
```

Unlike [the](https://www.npmjs.com/package/pkg-dir) [prior](https://www.npmjs.com/package/find-pkg) [arts](https://www.npmjs.com/package/find-root),

* It uses [`InternalModuleStat`](https://github.com/nodejs/node/blob/v10.1.0/src/node_file.cc#L798) through [`require.resolve()`](https://nodejs.org/api/modules.html#modules_require_resolve_request_options) as it's [faster](#benchmark) than [`fs.statSync()`](https://nodejs.org/api/fs.html#fs_fs_statsync_path).
* It checks if a path is file or directory, to avoid mistaking a `package.json` *directory* as a `package.json` file.
* It automatically resolves symbolic links.

## Installation

[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).

```
npm install find-pkg-dir
```

## API

```javascript
const findPkgDir = require('find-pkg-dir');
```

### findPkgDir(*path*)

*path*: `string` (a path to start searching from)  
Return: `string` (absolute path) or `null`

It finds the first directory containing a [`package.json` file](https://docs.npmjs.com/files/package.json), recursively looking up, starting with the given *path*.

When it cannot find any `package.json` files finally, returns `null`.

```javascript
findPkgDir('path/of/non/nodejs/project'); //=> null
```

### Benchmark

```
find-pkg-dir (this project):
Find from the current directory          6.514971999917179 ms/op avg.
Find from the deep directory           301.970978999976069 ms/op avg.
Resolve symlinks                         4.765490400046110 ms/op avg.
Find from the `package.json` directory  33.653173299971968 ms/op avg.

find-pkg + path.dirname():
Find from the current directory          7.597467000037431 ms/op avg.
Find from the deep directory           421.827792199980479 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)

find-root:
Find from the current directory          8.991230100020767 ms/op avg.
Find from the deep directory           479.851285400055360 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)

pkg-dir:
Find from the current directory          9.322520200069993 ms/op avg.
Find from the deep directory           505.923578500002634 ms/op avg.
Resolve symlinks                               N/A (operation failed)
Find from the `package.json` directory         N/A (operation failed)
```

## License

[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe

---
_Source: https://npm.io/package/find-pkg-dir · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
