What is pkg-up?
The pkg-up npm package is used to find the closest package.json file in the directory tree, starting from a given directory. It is useful for Node.js projects where you need to locate the package.json file of the current project or a related package.
What are pkg-up's main functionalities?
Find the closest package.json
This feature allows you to asynchronously find the nearest package.json file from the current directory.
const pkgUp = require('pkg-up');
(async () => {
const filePath = await pkgUp();
console.log(filePath);
//=> '/Users/sindresorhus/foo/package.json'
})();
Find the closest package.json synchronously
This feature provides a synchronous method to find the nearest package.json file.
const pkgUp = require('pkg-up');
const filePath = pkgUp.sync();
console.log(filePath);
//=> '/Users/sindresorhus/foo/package.json'
Find package.json from a specific directory
This feature allows you to find the nearest package.json file starting from a specific directory.
const pkgUp = require('pkg-up');
(async () => {
const filePath = await pkgUp({cwd: 'some/subdirectory'});
console.log(filePath);
//=> '/Users/sindresorhus/foo/package.json'
})();
Other packages similar to pkg-up
find-up
find-up is similar to pkg-up but is more generic. It allows you to find files or directories by name, moving upwards from a given directory. It's not limited to package.json files, making it more versatile for different use cases.
read-pkg-up
read-pkg-up is built on top of pkg-up and not only finds the closest package.json file but also reads and parses it. This package combines the functionality of pkg-up and read-pkg, providing a more comprehensive solution for accessing package information.
look-up
look-up is another package that can be used to find files in parent directories, similar to pkg-up. However, it is less focused on package.json files and more on file patterns, which can be useful for different kinds of file searches.
pkg-up
Find the closest package.json file
Install
$ npm install pkg-up
Usage
/
└── Users
└── sindresorhus
└── foo
├── package.json
└── bar
├── baz
└── example.js
const pkgUp = require('pkg-up');
(async () => {
console.log(await pkgUp());
})();
API
pkgUp([options])
Returns a Promise<string>
for the filepath, or Promise<null>
if it couldn't be found.
pkgUp.sync([options])
Returns the filepath, or null
if it couldn't be found.
options
Type: Object
cwd
Type: string
Default: process.cwd()
Directory to start from.
Related
- read-pkg-up - Read the closest package.json file
- pkg-dir - Find the root directory of an npm package
- find-up - Find a file by walking up parent directories
License
MIT © Sindre Sorhus