UNPKG

4.26 kBMarkdownView Raw
1# find-up
2
3> Find a file or directory by walking up parent directories
4
5## Install
6
7```sh
8npm install find-up
9```
10
11## Usage
12
13```
14/
15└── Users
16 └── sindresorhus
17 ├── unicorn.png
18 └── foo
19 └── bar
20 ├── baz
21 └── example.js
22```
23
24`example.js`
25
26```js
27import path from 'node:path';
28import {findUp, pathExists} from 'find-up';
29
30console.log(await findUp('unicorn.png'));
31//=> '/Users/sindresorhus/unicorn.png'
32
33console.log(await findUp(['rainbow.png', 'unicorn.png']));
34//=> '/Users/sindresorhus/unicorn.png'
35
36console.log(await findUp(async directory => {
37 const hasUnicorns = await pathExists(path.join(directory, 'unicorn.png'));
38 return hasUnicorns && directory;
39}, {type: 'directory'}));
40//=> '/Users/sindresorhus'
41```
42
43## API
44
45### findUp(name, options?)
46### findUp(matcher, options?)
47
48Returns a `Promise` for either the path or `undefined` if it could not be found.
49
50### findUp([...name], options?)
51
52Returns a `Promise` for either the first path found (by respecting the order of the array) or `undefined` if none could be found.
53
54### findUpMultiple(name, options?)
55### findUpMultiple(matcher, options?)
56
57Returns a `Promise` for either an array of paths or an empty array if none could be found.
58
59### findUpMultiple([...name], options?)
60
61Returns a `Promise` for either an array of the first paths found (by respecting the order of the array) or an empty array if none could be found.
62
63### findUpSync(name, options?)
64### findUpSync(matcher, options?)
65
66Returns a path or `undefined` if it could not be found.
67
68### findUpSync([...name], options?)
69
70Returns the first path found (by respecting the order of the array) or `undefined` if none could be found.
71
72### findUpMultipleSync(name, options?)
73### findUpMultipleSync(matcher, options?)
74
75Returns an array of paths or an empty array if none could be found.
76
77### findUpMultipleSync([...name], options?)
78
79Returns an array of the first paths found (by respecting the order of the array) or an empty array if none could be found.
80
81#### name
82
83Type: `string`
84
85The name of the file or directory to find.
86
87#### matcher
88
89Type: `Function`
90
91A 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.
92
93When using async mode, the `matcher` may optionally be an async or promise-returning function that returns the path.
94
95#### options
96
97Type: `object`
98
99##### cwd
100
101Type: `URL | string`\
102Default: `process.cwd()`
103
104The directory to start from.
105
106##### type
107
108Type: `string`\
109Default: `'file'`\
110Values: `'file' | 'directory'`
111
112The type of path to match.
113
114##### allowSymlinks
115
116Type: `boolean`\
117Default: `true`
118
119Allow symbolic links to match if they point to the chosen path type.
120
121##### stopAt
122
123Type: `URL | string`\
124Default: Root directory
125
126A directory path where the search halts if no matches are found before reaching this point.
127
128### pathExists(path)
129
130Returns a `Promise<boolean>` of whether the path exists.
131
132### pathExistsSync(path)
133
134Returns a `boolean` of whether the path exists.
135
136#### path
137
138Type: `string`
139
140The path to a file or directory.
141
142### findUpStop
143
144A [`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.
145
146```js
147import path from 'node:path';
148import {findUp, findUpStop} from 'find-up';
149
150await findUp(directory => {
151 return path.basename(directory) === 'work' ? findUpStop : 'logo.png';
152});
153```
154
155## Related
156
157- [find-up-cli](https://github.com/sindresorhus/find-up-cli) - CLI for this module
158- [package-up](https://github.com/sindresorhus/package-up) - Find the closest package.json file
159- [pkg-dir](https://github.com/sindresorhus/pkg-dir) - Find the root directory of an npm package
160- [resolve-from](https://github.com/sindresorhus/resolve-from) - Resolve the path of a module like `require.resolve()` but from a given path