UNPKG

4.04 kBMarkdownView Raw
1# find-up [![Build Status](https://travis-ci.org/sindresorhus/find-up.svg?branch=master)](https://travis-ci.org/sindresorhus/find-up)
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
29const path = require('path');
30const findUp = require('find-up');
31
32(async () => {
33 console.log(await findUp('unicorn.png'));
34 //=> '/Users/sindresorhus/unicorn.png'
35
36 console.log(await findUp(['rainbow.png', 'unicorn.png']));
37 //=> '/Users/sindresorhus/unicorn.png'
38
39 console.log(await findUp(async directory => {
40 const hasUnicorns = await findUp.exists(path.join(directory, 'unicorn.png'));
41 return hasUnicorns && directory;
42 }, {type: 'directory'}));
43 //=> '/Users/sindresorhus'
44})();
45```
46
47
48## API
49
50### findUp(name, options?)
51### findUp(matcher, options?)
52
53Returns a `Promise` for either the path or `undefined` if it couldn't be found.
54
55### findUp([...name], options?)
56
57Returns a `Promise` for either the first path found (by respecting the order of the array) or `undefined` if none could be found.
58
59### findUp.sync(name, options?)
60### findUp.sync(matcher, options?)
61
62Returns a path or `undefined` if it couldn't be found.
63
64### findUp.sync([...name], options?)
65
66Returns the first path found (by respecting the order of the array) or `undefined` if none could be found.
67
68#### name
69
70Type: `string`
71
72Name of the file or directory to find.
73
74#### matcher
75
76Type: `Function`
77
78A function that will be called with each directory until it returns a `string` with the path, which stops the search, or the root directory has been reached and nothing was found. Useful if you want to match files with certain patterns, set of permissions, or other advanced use-cases.
79
80When using async mode, the `matcher` may optionally be an async or promise-returning function that returns the path.
81
82#### options
83
84Type: `object`
85
86##### cwd
87
88Type: `string`<br>
89Default: `process.cwd()`
90
91Directory to start from.
92
93##### type
94
95Type: `string`<br>
96Default: `'file'`<br>
97Values: `'file'` `'directory'`
98
99The type of paths that can match.
100
101##### allowSymlinks
102
103Type: `boolean`<br>
104Default: `true`
105
106Allow symbolic links to match if they point to the chosen path type.
107
108### findUp.exists(path)
109
110Returns a `Promise<boolean>` of whether the path exists.
111
112### findUp.sync.exists(path)
113
114Returns a `boolean` of whether the path exists.
115
116#### path
117
118Type: `string`
119
120Path to a file or directory.
121
122### findUp.stop
123
124A [`Symbol`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol) that can be returned by a `matcher` function to stop the search and cause `findUp` to immediately return `undefined`. Useful as a performance optimization in case the current working directory is deeply nested in the filesystem.
125
126```js
127const path = require('path');
128const findUp = require('find-up');
129
130(async () => {
131 await findUp(directory => {
132 return path.basename(directory) === 'work' ? findUp.stop : 'logo.png';
133 });
134})();
135```
136
137
138## Related
139
140- [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module
141- [pkg-up](https://github.com/sindresorhus/pkg-up) - Find the closest package.json file
142- [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package
143- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path
144
145
146---
147
148<div align="center">
149 <b>
150 <a href="https://tidelift.com/subscription/pkg/npm-find-up?utm_source=npm-find-up&utm_medium=referral&utm_campaign=readme">Get professional support for 'find-up' with a Tidelift subscription</a>
151 </b>
152 <br>
153 <sub>
154 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
155 </sub>
156</div>