2020-03-22 05:13:25 +08:00
# find-up [![Build Status: Linux and macOS](https://travis-ci.org/sindresorhus/find-up.svg?branch=master)](https://travis-ci.org/sindresorhus/find-up) [![Build Status: Windows](https://ci.appveyor.com/api/projects/status/l0cyjmvh5lq72vq2/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/find-up/branch/master)
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
> Find a file by walking up parent directories
2020-01-28 13:07:56 +08:00
## Install
```
2020-03-22 05:13:25 +08:00
$ npm install --save find-up
2020-01-28 13:07:56 +08:00
```
## Usage
```
/
└── Users
2020-03-22 05:13:25 +08:00
└── sindresorhus
├── unicorn.png
└── foo
└── bar
├── baz
└── example.js
2020-01-28 13:07:56 +08:00
```
```js
2020-03-22 05:13:25 +08:00
// example.js
2020-01-28 13:07:56 +08:00
const findUp = require('find-up');
2020-03-22 05:13:25 +08:00
findUp('unicorn.png').then(filepath => {
console.log(filepath);
2020-01-28 13:07:56 +08:00
//=> '/Users/sindresorhus/unicorn.png'
2020-03-22 05:13:25 +08:00
});
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
findUp(['rainbow.png', 'unicorn.png']).then(filepath => {
console.log(filepath);
2020-01-28 13:07:56 +08:00
//=> '/Users/sindresorhus/unicorn.png'
2020-03-22 05:13:25 +08:00
});
2020-01-28 13:07:56 +08:00
```
## API
2020-03-22 05:13:25 +08:00
### findUp(filename, [options])
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
Returns a `Promise` for the filepath or `null` .
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
### findUp([filenameA, filenameB], [options])
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
Returns a `Promise` for the first filepath found (by respecting the order) or `null` .
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
### findUp.sync(filename, [options])
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
Returns a filepath or `null` .
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
### findUp.sync([filenameA, filenameB], [options])
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
Returns the first filepath found (by respecting the order) or `null` .
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
#### filename
2020-01-28 13:07:56 +08:00
Type: `string`
2020-03-22 05:13:25 +08:00
Filename of the file to find.
2020-01-28 13:07:56 +08:00
#### options
##### cwd
Type: `string` < br >
Default: `process.cwd()`
Directory to start from.
## Related
- [find-up-cli ](https://github.com/sindresorhus/find-up-cli ) - CLI for this module
- [pkg-up ](https://github.com/sindresorhus/pkg-up ) - Find the closest package.json file
- [pkg-dir ](https://github.com/sindresorhus/pkg-dir ) - Find the root directory of an npm package
2020-03-22 05:13:25 +08:00
## License
2020-01-28 13:07:56 +08:00
2020-03-22 05:13:25 +08:00
MIT © [Sindre Sorhus ](https://sindresorhus.com )