find-up
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
locate-path | 7.2.0 | 2.83 kB | MIT | prod | |
path-exists | 5.0.0 | 2.04 kB | MIT | prod |
Find-up is a useful Node.js package that allows you to find a file or directory by walking up parent directories. It provides a flexible and efficient solution to locate files or directories from a given starting path, with a multitude of custom options to enhance search functionality.
To use find-up, first, install the package using npm with the command $ npm install find-up
. Once installed, you can import the necessary functions from the find-up module in your JavaScript file. Here are a few examples of how to use find-up:
For finding a file:
import {findUp} from 'find-up';
console.log(await findUp('unicorn.png'));
// Output: '/Users/sindresorhus/unicorn.png'
For finding either one of the listed files (returns the first one found):
console.log(await findUp(['rainbow.png', 'unicorn.png']));
// Output: '/Users/sindresorhus/unicorn.png'
For custom matcher function:
import path from 'node:path';
import {findUp, pathExists} from 'find-up';
console.log(await findUp(async directory => {
const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
return hasUnicorns && directory;
}, {type: 'directory'}));
// Output: '/Users/sindresorhus'
The return values of the functions are promises either for the file path if found, or undefined if not found.
The detailed documentation for find-up, including all its parameters and options, is available directly in the readme content provided at the GitHub page https://github.com/sindresorhus/find-up. It explains in-detail about all the methods, options, matchers and usage examples.