# find-root

> find the closest package.json

Latest version **1.1.0** (published 2017-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install find-root
pnpm add find-root
yarn add find-root
bun add find-root
```

## 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 | 1.1.0 |
| Published | 2017-06-29 |
| First published | 2013-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/find-root) |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 47 |
| Author | jsdnxx |
| Maintainers | jsdnxx |
| Keywords | fs, get, find, closest, package, module, base, root |

## Links

- npm: https://www.npmjs.com/package/find-root
- Repository: https://github.com/js-n/find-root
- Homepage: https://github.com/js-n/find-root#readme
- Issues: https://github.com/js-n/find-root/issues
- npm.io page: https://npm.io/package/find-root

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2017-06-29
- 1.0.0 — 2016-02-21
- 0.1.2 — 2016-02-21
- 0.1.1 — 2013-12-05
- 0.1.0 — 2013-12-05

## README

# find-root
recursively find the closest package.json

[![Build Status](https://travis-ci.org/js-n/find-root.svg?branch=master)](https://travis-ci.org/js-n/find-root)

## usage
Say you want to check if the directory name of a project matches its
module name in package.json:

```js
const path = require('path')
const findRoot = require('find-root')

// from a starting directory, recursively search for the nearest
// directory containing package.json
const root = findRoot('/Users/jsdnxx/Code/find-root/tests')
// => '/Users/jsdnxx/Code/find-root'

const dirname = path.basename(root)
console.log('is it the same?')
console.log(dirname === require(path.join(root, 'package.json')).name)
```

You can also pass in a custom check function (by default, it checks for the
existence of `package.json` in a directory). In this example, we traverse up
to find the root of a git repo:
```js
const fs = require('fs')

const gitRoot = findRoot('/Users/jsdnxx/Code/find-root/tests', function (dir) {
  return fs.existsSync(path.resolve(dir, '.git'))
})
```


## api

### `findRoot: (startingPath : string, check?: (dir: string) => boolean) => string`

Returns the path for the nearest directory to `startingPath` containing
a `package.json` file, eg `/foo/module`.

If `check` is provided, returns the path for the closest parent directory
where `check` returns true.

Throws an error if no `package.json` is found at any level in the
`startingPath`.


## installation
```sh
> npm install find-root
```

## running the tests

From package root:
```sh
> npm install
> npm test
```

## contributors

- jsdnxx


## license
MIT. (c) 2017 jsdnxx

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