1 | # find-up
|
2 |
|
3 | > Find a file or directory by walking up parent directories
|
4 |
|
5 | ## Install
|
6 |
|
7 | ```sh
|
8 | npm 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
|
27 | import path from 'node:path';
|
28 | import {findUp, pathExists} from 'find-up';
|
29 |
|
30 | console.log(await findUp('unicorn.png'));
|
31 | //=> '/Users/sindresorhus/unicorn.png'
|
32 |
|
33 | console.log(await findUp(['rainbow.png', 'unicorn.png']));
|
34 | //=> '/Users/sindresorhus/unicorn.png'
|
35 |
|
36 | console.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 |
|
48 | Returns a `Promise` for either the path or `undefined` if it could not be found.
|
49 |
|
50 | ### findUp([...name], options?)
|
51 |
|
52 | Returns 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 |
|
57 | Returns a `Promise` for either an array of paths or an empty array if none could be found.
|
58 |
|
59 | ### findUpMultiple([...name], options?)
|
60 |
|
61 | Returns 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 |
|
66 | Returns a path or `undefined` if it could not be found.
|
67 |
|
68 | ### findUpSync([...name], options?)
|
69 |
|
70 | Returns 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 |
|
75 | Returns an array of paths or an empty array if none could be found.
|
76 |
|
77 | ### findUpMultipleSync([...name], options?)
|
78 |
|
79 | Returns 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 |
|
83 | Type: `string`
|
84 |
|
85 | The name of the file or directory to find.
|
86 |
|
87 | #### matcher
|
88 |
|
89 | Type: `Function`
|
90 |
|
91 | A 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 |
|
93 | When using async mode, the `matcher` may optionally be an async or promise-returning function that returns the path.
|
94 |
|
95 | #### options
|
96 |
|
97 | Type: `object`
|
98 |
|
99 | ##### cwd
|
100 |
|
101 | Type: `URL | string`\
|
102 | Default: `process.cwd()`
|
103 |
|
104 | The directory to start from.
|
105 |
|
106 | ##### type
|
107 |
|
108 | Type: `string`\
|
109 | Default: `'file'`\
|
110 | Values: `'file' | 'directory'`
|
111 |
|
112 | The type of path to match.
|
113 |
|
114 | ##### allowSymlinks
|
115 |
|
116 | Type: `boolean`\
|
117 | Default: `true`
|
118 |
|
119 | Allow symbolic links to match if they point to the chosen path type.
|
120 |
|
121 | ##### stopAt
|
122 |
|
123 | Type: `URL | string`\
|
124 | Default: Root directory
|
125 |
|
126 | A directory path where the search halts if no matches are found before reaching this point.
|
127 |
|
128 | ### pathExists(path)
|
129 |
|
130 | Returns a `Promise<boolean>` of whether the path exists.
|
131 |
|
132 | ### pathExistsSync(path)
|
133 |
|
134 | Returns a `boolean` of whether the path exists.
|
135 |
|
136 | #### path
|
137 |
|
138 | Type: `string`
|
139 |
|
140 | The path to a file or directory.
|
141 |
|
142 | ### findUpStop
|
143 |
|
144 | A [`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
|
147 | import path from 'node:path';
|
148 | import {findUp, findUpStop} from 'find-up';
|
149 |
|
150 | await 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
|