You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

4 years ago
  1. # path-exists [![Build Status](https://travis-ci.org/sindresorhus/path-exists.svg?branch=master)](https://travis-ci.org/sindresorhus/path-exists)
  2. > Check if a path exists
  3. 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.
  4. Never use this before handling a file though:
  5. > 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.
  6. ## Install
  7. ```
  8. $ npm install --save path-exists
  9. ```
  10. ## Usage
  11. ```js
  12. // foo.js
  13. const pathExists = require('path-exists');
  14. pathExists('foo.js').then(exists => {
  15. console.log(exists);
  16. //=> true
  17. });
  18. ```
  19. ## API
  20. ### pathExists(path)
  21. Returns a promise for a boolean of whether the path exists.
  22. ### pathExists.sync(path)
  23. Returns a boolean of whether the path exists.
  24. ## Related
  25. - [path-exists-cli](https://github.com/sindresorhus/path-exists-cli) - CLI for this module
  26. ## License
  27. MIT © [Sindre Sorhus](https://sindresorhus.com)