# findup-package-json

> Find the closest package.json file meeting specific criteria by searching upwards from a given directory until hitting root

Latest version **4.1.1** (published 2015-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install findup-package-json
pnpm add findup-package-json
yarn add findup-package-json
bun add findup-package-json
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.1 |
| Published | 2015-12-22 |
| First published | 2015-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 8 |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | serapath |
| Keywords | browserify, transform, package, json, parse, find, up, closest |

## Links

- npm: https://www.npmjs.com/package/findup-package-json
- Repository: https://github.com/serapath/findup-package-json
- Issues: https://github.com/serapath/findup-package-json/issues
- npm.io page: https://npm.io/package/findup-package-json

## Dependencies (8)

- [getval](https://npm.io/package/getval.md) ^1.0.0
- [callify](https://npm.io/package/callify.md) ^0.3.0
- [defined](https://npm.io/package/defined.md) ^1.0.0
- [falafel](https://npm.io/package/falafel.md) ^1.2.0
- [isarray](https://npm.io/package/isarray.md) 0.0.1
- [through2](https://npm.io/package/through2.md) ^2.0.0
- [exemethod](https://npm.io/package/exemethod.md) ^1.3.0
- [static-eval](https://npm.io/package/static-eval.md) ^0.2.4

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 4.1.1 (latest) — 2015-12-22
- 4.1.0 — 2015-12-22
- 4.0.0 — 2015-11-15
- 2.0.0 — 2015-10-14

## README

# findup-package-json [![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

Find the closest package.json file meeting specific criteria by searching upwards from a given directory until hitting root.

## Usage

[![NPM](https://nodei.co/npm/findup-package-json.png)](https://nodei.co/npm/findup-package-json/)

Can be used as a regular module and/or a browserify transform to inline specific fields of a projects 'package.json'

### `findup([fields], [dir], [filter], found(err, packageJson))`

Given a starting directory `dir`, look up through every directory to see if it contains a `package.json` file matching the `filter` function, for example:

``` javascript
var findup = require('findup-package-json')
/******************************************************************************
  PARAMETER:
    findup({
      fields: optional array,
        * array of fields you want from package.json
        * defaults to ALL FIELDS if not provided
      path: optional string,
        * the path where the search starts, going one level up towards the root directory and returns the package.json content if it can find it
        * defaults to 'process.cwd()'
      filter: optional function,
        * a function (json, filename) in which you can test the content and return true if it's the right packageJson, otherwise return false to continue the search.
        * defaults to function (packageJson) { return true } to return the first found
      found: required callback function
        * a callback (err, packageJson) that will be called with the content of the found package.json which has all the fields you asked for
    })

******************************************************************************/

// Below a list of all different ways to call "findup"

findup({ found: function (err, result) { // required callback
  console.log(result.pkgfile)
  console.log(result.pkg)
}})
// or
findup({
  fields: ['name', 'scripts.test'], // optional, default: all fields
  dirname: __dirname, // optional, default: process.cwd()
  filter: function (json, filename) { // optional, default: return true
    return json.name === 'async'
  },
  found: function (err, result) { // required callback
    console.log(result.pkgfile)
    console.log(result.pkg)
  }
})
```

Note that `filter` is optional and takes the following arguments:

* `json`: the parsed `package.json` file.
* `filename`: the `package.json`'s absolute filename.

### `file = findup.sync([fields], [dir], [filter])`

Same as the `findup` function, however executed synchronously:

``` javascript
var findup = require('findup-package-json')
/******************************************************************************
  PARAMETER:
    var found = findup.sync({
      fields: optional array,
        * array of fields you want from package.json
        * defaults to ALL FIELDS if not provided
      path: optional string,
        * the path where the search starts, going one level up towards the root directory and returns the package.json content if it can find it
        * defaults to 'process.cwd()'
      filter: optional function
        * a function (json, filename) in which you can test the content and return true if it's the right packageJson, otherwise return false to continue the search.
        * defaults to function (packageJson) { return true } to return the first found
    }) // returns the content of the found package.json which has all the fields you asked for

******************************************************************************/
// Below a list of all different ways to call "findup"

var result = findup.sync()
// or
var result = findup.sync({
  fields: ['name', 'scripts.test'], // optional, default: all fields
  dirname: __dirname, // optional, default: process.cwd()
  filter: function (json, filename) { // optional, default: return true
    return json.name === 'async'
  }
})
```

### browserify

Turns the calls to `require('findup-package-json')` as shown above and the selected `package.json` fields into an inlined object that goes into the `bundle.js`, but requires the arguments to `findup` or `findup.sync` to be statically analysable

It only works if the call is directly made on the require statement

```js
require('findup-package-json')({ found: function (err, json) {
  // ... use err and/or json
  console.log(json.pkgfile)
  console.log(json.pkg)
}})
// or
var json = require('findup-package-json').sync(/*...*/)
console.log(json.pkgfile)
console.log(json.pkg)
```
after `browserify index.js -t findup-package-json > bundle.js`
will be turned into

```js
(function (err, json) {
  // ... use err and/or json
  console.log(json.pkgfile)
  console.log(json.pkg)
})({
  pkgfile   : '...',
  pkg       : { /* package.json with requested fields */ }
})
// or
var json =  {
  pkgfile   : '...',
  pkg       : { /* package.json with requested fields */ }
}
console.log(json.pkgfile)
console.log(json.pkg)
```
## License

MIT. See [LICENSE.md](http://github.com/hughsk/findup-package-json/blob/master/LICENSE.md) for details.

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