1 |
3a515b92
|
cagy
|
# path-exists [](https://travis-ci.org/sindresorhus/path-exists)
|
2 |
|
|
|
3 |
|
|
> Check if a path exists
|
4 |
|
|
|
5 |
|
|
Because [`fs.exists()`](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback) is being [deprecated](https://github.com/iojs/io.js/issues/103), but there's still a genuine use-case of being able to check if a path exists for other purposes than doing IO with it.
|
6 |
|
|
|
7 |
|
|
Never use this before handling a file though:
|
8 |
|
|
|
9 |
|
|
> In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to `fs.exists()` and `fs.open()`. Just open the file and handle the error when it's not there.
|
10 |
|
|
|
11 |
|
|
|
12 |
|
|
## Install
|
13 |
|
|
|
14 |
|
|
```
|
15 |
|
|
$ npm install --save path-exists
|
16 |
|
|
```
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
## Usage
|
20 |
|
|
|
21 |
|
|
```js
|
22 |
|
|
// foo.js
|
23 |
|
|
const pathExists = require('path-exists');
|
24 |
|
|
|
25 |
|
|
pathExists('foo.js').then(exists => {
|
26 |
|
|
console.log(exists);
|
27 |
|
|
//=> true
|
28 |
|
|
});
|
29 |
|
|
```
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
## API
|
33 |
|
|
|
34 |
|
|
### pathExists(path)
|
35 |
|
|
|
36 |
|
|
Returns a promise for a boolean of whether the path exists.
|
37 |
|
|
|
38 |
|
|
### pathExists.sync(path)
|
39 |
|
|
|
40 |
|
|
Returns a boolean of whether the path exists.
|
41 |
|
|
|
42 |
|
|
|
43 |
|
|
## Related
|
44 |
|
|
|
45 |
|
|
- [path-exists-cli](https://github.com/sindresorhus/path-exists-cli) - CLI for this module
|
46 |
|
|
|
47 |
|
|
|
48 |
|
|
## License
|
49 |
|
|
|
50 |
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|