npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

findup-package-json

v4.1.1

Published

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

Downloads

7

Readme

findup-package-json experimental

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

Usage

NPM

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:

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:

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

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

(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 for details.