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 fs = require('fs');
const path = require('path');
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 => {
return fs.existsSync(path.join(dir, 'unicorn.png')) && 'foo';
}));
//=> '/Users/sindresorhus/foo'
console.log(await findUp(async dir => {
const children = await fs.promises.readdir(dir);
if (children.some(fileName => fileName.endsWith('.png'))) {
return 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
if none could be found.
Type: string
Filename of the file to find.
Type: Function
A function that will 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. Useful if you want to match files with a certain pattern, set of permissions, or other advanced use cases.
When using async mode, matcher
may optionally be an async
function or return a Promise
for the filepath.
Type: Object
Type: string
Default: process.cwd()
Directory to start from.
To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.
- 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