Contents
Introduction
Returns the path and contents of the root-most package.json
.
Package manager agnostic.
Useful for situations where multiple package.json
s may exist in parent directories, due to node_modules
or workspaces.
Install
npm i root-package-json
Example
Given file structure
/
└─┬ root
├─┬ node_modules
│ └─┬ root-package-json
│ └── package.json // { name: 'root-package-json' }
├─┬ packages
│ └─┬ my-package
│ ├── my-file.js
│ └── package.json // { name: 'my-package' }
└── package.json // { name: 'monorepo-root' }
└── example.js
my-file.js
import { rootPackageJson } from 'root-package-json';
const rootPackage = await rootPackageJson();
console.log(rootPackage);
// {
// filePath: '/root/package.json',
// package: { name: 'monorepo-root' }
// }
Usage
root-package-json
is an ESM module. That means it must be import
ed. To load from a CJS module, use dynamic import const { rootPackageJson } = await import('root-package-json');
.
API
rootPackageJson(options?)
Returns promise that resolves to either the root package.json, or null if none can be found.
options
- cwd
- Type:
string
orURL
- optional, defaults to
process.cwd()
- Directory to use as base directory.
- See
parse-cwd
.
- Type: