findpkg

Finds the package.json file for a module

npm install findpkg
15 downloads in the last day
116 downloads in the last week
312 downloads in the last month

findpkg

Finds the package descriptor file for a module.

Overview

Node.js packages are specified by a directory containing a package.json descriptor file. The top-level package directory as well as the package name are frequently required by logging utilities and configuration file managers. The findpkg function returns an object providing this information.

Features

The findpkg utility offers the following features:

  1. Given a module, finds the directory containing the package.json file.
  2. Parses the package name from the package.json file.
  3. Provides a resolve method that resolves filenames against the package directory (includes special handling of filenames prefixed with a dot).
  4. Provides fallback behavior if the module is not part of a package.

Installation

Install using NPM.

npm install findpkg

(Or add it to your dependencies.)

API

Calling require('findpkg') returns a function taking a module.

var findpkg = require('findpkg');
var pkginfo = findpkg(module);

Parameters:

  • module - {Object} The module (i.e., module or module.parent).

Returns:

Returns an object having the following five components:

  1. found - {Boolean} Set to true if a package descriptor file was found. (See API notes below.)
  2. name - {String} The name property from the package descriptor file.
  3. pathname - {String} The pathname of the package descriptor file.
  4. dirname - {String} The directory name of the package descriptor file (the top-level directory of the package).
  5. resolve - {Function} Resolves a filename against the dirname unless the filename is prefixed with a dot (i.e., ./ or ../), in which case the current working directory is used.

Errors:

  • Throws an exception if the argument is not a module or if the package descriptor file (if found) cannot be parsed.

Note:

If no package.json file is found, then the return value is populated from the module parameter as follows:

  • The found property is set to false.
  • The name is set to the basename portion of the module's filename (without the extension).
  • The pathname is set to the value of the module's filename.
  • The dirname is set to the directory portion of the module's filename.
  • The resolve function still resolves against the dirname (no change).

License

(The MIT License)

Copyright (c) 2012 Frank Hellwig

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

npm loves you