1
|
# 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)
|
2
|
|
3
|
> Find a file or directory by walking up parent directories
|
4
|
|
5
|
|
6
|
## Install
|
7
|
|
8
|
```
|
9
|
$ npm install find-up
|
10
|
```
|
11
|
|
12
|
|
13
|
## Usage
|
14
|
|
15
|
```
|
16
|
/
|
17
|
└── Users
|
18
|
└── sindresorhus
|
19
|
├── unicorn.png
|
20
|
└── foo
|
21
|
└── bar
|
22
|
├── baz
|
23
|
└── example.js
|
24
|
```
|
25
|
|
26
|
`example.js`
|
27
|
|
28
|
```js
|
29
|
const findUp = require('find-up');
|
30
|
|
31
|
(async () => {
|
32
|
console.log(await findUp('unicorn.png'));
|
33
|
//=> '/Users/sindresorhus/unicorn.png'
|
34
|
|
35
|
console.log(await findUp(['rainbow.png', 'unicorn.png']));
|
36
|
//=> '/Users/sindresorhus/unicorn.png'
|
37
|
})();
|
38
|
```
|
39
|
|
40
|
|
41
|
## API
|
42
|
|
43
|
### findUp(filename, [options])
|
44
|
|
45
|
Returns a `Promise` for either the filepath or `null` if it couldn't be found.
|
46
|
|
47
|
### findUp([filenameA, filenameB], [options])
|
48
|
|
49
|
Returns a `Promise` for either the first filepath found (by respecting the order) or `null` if none could be found.
|
50
|
|
51
|
### findUp.sync(filename, [options])
|
52
|
|
53
|
Returns a filepath or `null`.
|
54
|
|
55
|
### findUp.sync([filenameA, filenameB], [options])
|
56
|
|
57
|
Returns the first filepath found (by respecting the order) or `null`.
|
58
|
|
59
|
#### filename
|
60
|
|
61
|
Type: `string`
|
62
|
|
63
|
Filename of the file to find.
|
64
|
|
65
|
#### options
|
66
|
|
67
|
Type: `Object`
|
68
|
|
69
|
##### cwd
|
70
|
|
71
|
Type: `string`<br>
|
72
|
Default: `process.cwd()`
|
73
|
|
74
|
Directory to start from.
|
75
|
|
76
|
|
77
|
## Related
|
78
|
|
79
|
- [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module
|
80
|
- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
|
81
|
- [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package
|
82
|
- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path
|
83
|
|
84
|
|
85
|
## License
|
86
|
|
87
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|