UNPKG

exists-file

Version:

Check if a file exists. A fs.exists implementation that is not deprecated.

70 lines (43 loc) 2.49 kB
# exists-file ![Last version](https://img.shields.io/github/tag/Kikobeats/exists-file.svg?style=flat-square) [![Build Status](http://img.shields.io/travis/Kikobeats/exists-file/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/exists-file) [![Coverage Status](https://img.shields.io/coveralls/Kikobeats/exists-file.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/exists-file) [![Dependency status](http://img.shields.io/david/Kikobeats/exists-file.svg?style=flat-square)](https://david-dm.org/Kikobeats/exists-file) [![Dev Dependencies Status](http://img.shields.io/david/dev/Kikobeats/exists-file.svg?style=flat-square)](https://david-dm.org/Kikobeats/exists-file#info=devDependencies) [![NPM Status](http://img.shields.io/npm/dm/exists-file.svg?style=flat-square)](https://www.npmjs.org/package/exists-file) [![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/kikobeats) > Check if a file exists. A fs.exists implementation that is not deprecated. Because [fs.exist](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback) and [fs.existsSync](https://nodejs.org/api/fs.html#fs_fs_existssync_path) are deprecated this an implementation using [fs.stats](https://nodejs.org/api/fs.html#fs_fs_stat_path_callback) and [fs.statsSync](https://nodejs.org/api/fs.html#fs_fs_statsync_path) for get the same result. Also **always** keep in mind: > fs.exists() should not be used to check if a file exists before calling fs.open(). Doing so introduces a race condition since other processes may change the file's state between the two calls. Instead, user code should call fs.open() directly and handle the error raised if the file is non-existent. ## Install ```bash npm install exists-file --save ``` ## Usage ```js var existsFile = require('exists-file') // async with a callback existsFile('./README.md', console.log) // => null, true // async with a promise existsFile('./README.md').then(console.log).catch(console.error) // => true // sync var exists = existsFile.sync('./README.md') console.log(exists) // => true ``` ## API ### existsFile(filepath, [cb]) #### filepath *Required* <br> Type: `string` The relative or absolute file path. #### cb Type: `function` If `cb` is not provided then it returns a `promise`. ### existsFile.sync(filepath) #### filepath *Required* <br> Type: `string` The relative or absolute file path. ## License MIT © [Kiko Beats](https://www.kikobeats.com)