Find a file or directory by walking up parent directories
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
$ npm install find-up
/
└── Users
└── sindresorhus
├── unicorn.png
└── foo
└── bar
├── baz
└── example.js
example.js
const findUp = require('find-up');
(async () => {
console.log(await findUp('unicorn.png'));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(['rainbow.png', 'unicorn.png']));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(dir => Promise.resolve('unicorn.png')));
//=> '/Users/sindresorhus/unicorn.png'
console.log(await findUp(dir => dir));
//=> '/Users/sindresorhus'
})();
Returns a Promise
for either the filepath or null
if it couldn't be found.
Returns a Promise
for either the first filepath found (by respecting the order) or null
if none could be found.
Returns a filepath or null
.
Returns the first filepath found (by respecting the order) or null
.
Type: string
Function
Filename of the file to find, or a custom matcher function to be called with each directory until it returns a filepath to stop the search or the root directory has been reached and nothing was found. When using a matcher function, if you want to check whether a file exists, use fs.access()
- this is done automatically when filename
is a string.
When using async mode, filename
may optionally be an async
function or return a Promise
for the filepath.
Type: Object
Type: string
Default: process.cwd()
Directory to start from.
- find-up-cli - CLI for this module
- pkg-up - Find the closest package.json file
- pkg-dir - Find the root directory of an npm package
- resolve-from - Resolve the path of a module like
require.resolve()
but from a given path
MIT © Sindre Sorhus