parent-package-json
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

parent-package-json

Find, read and parse the package.json that sits above your module. Provide a custom ignore count or start path to define where to look for a parent package.

npm install --save parent-package-json
import parentJSON from "parent-package-json";

Getting Started

To get the nearest parent package.jsons path, content (as string) or parsed content (using JSON.parse):

const parent = parentJSON();

const pathToParentPackageJSON = parent.path.relative;
const parentContentAsString = parent.read();
const parentContentAsObject = parent.parse();

const parentVersion = parentContentAsObject.version;

The parent package.json is looked up starting from the current working directory of your script. If none is found, paths, .read() and .parse() will return undefined.

Custom Path and Ignore Count

If you need to determine the parent package.json of a custom path instead of the current working directory, which should be the module from which you are running the code, you can specify it via the path option:

const startPath = path.join(...);
const parent = parent({ startPath });

You can also specify a count of parent directories to skip:

const parent = parent({ ignoreCount: 1 });

Note: A package.json file in the provided startPath is always ignored. Set the startPath one layer below the one where you expect to find a parent package file.

Package Sidebar

Install

npm i parent-package-json

Weekly Downloads

6,237

Version

3.0.0

License

ISC

Unpacked Size

7.32 kB

Total Files

12

Last publish

Collaborators

  • maxrimue