33

I need to check if a file exists in a gulp task, i know i can use some node functions from node, there are two:

fs.exists() and fs.existsSync()

The problem is that in the node documentation, is saying that these functions will be deprecated

Caio Kawasaki
  • 2,894
  • 6
  • 33
  • 69
  • 1
    Possible duplicate of [Check synchronously if file/directory exists in Node.js](https://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js) – Leland Jun 01 '17 at 13:53

5 Answers5

45

You can use fs.access

fs.access('/etc/passwd', (err) => {
    if (err) {
        // file/path is not visible to the calling process
        console.log(err.message);
        console.log(err.code);
    }
});

List of available error codes here


Using fs.access() to check for the accessibility of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Doing so introduces a race condition, since other processes may change the file's state between the two calls. Instead, user code should open/read/write the file directly and handle the error raised if the file is not accessible.

Alexander
  • 12,424
  • 5
  • 59
  • 76
  • The current node documentatión [does not recommend using stat to check wether a file exists][1]: [1]: https://nodejs.org/api/fs.html#fs_fs_stat_path_callback – David Lemon Jul 21 '17 at 09:23
4

You could add

var f;

try {
  var f = require('your-file');
} catch (error) {

  // ....
}

if (f) {
  console.log(f);
}
Raulucco
  • 3,406
  • 1
  • 21
  • 27
3

As of 2018, you can use fs.existsSync():

fs.exists() is deprecated, but fs.existsSync() is not. The callback parameter to fs.exists() accepts parameters that are inconsistent with other Node.js callbacks. fs.existsSync() does not use a callback.

See this answer for more details.

Jack Steam
  • 4,500
  • 1
  • 24
  • 39
1

I believe fs-access package has been depreciated alternatively you may want to use:

path-exists.

file-exists.

Intracutions (path-exists):

npm install path-exists --save

const myFile = '/my_file_to_ceck.html';
const exists = pathExists.sync(myFile);
console.log(exists);

Intracutions (file-exists):

npm install file-exists --save


const fileExists = require('file-exists');
const myFile = '/my_file_to_ceck.html';
fileExists(myFile, (err, exists) => console.log(exists))

NPM Link: path exists

NPM Link: file exists

Ithar
  • 4,865
  • 4
  • 39
  • 40
0

The node documentatión does not recommend using stat to check wether a file exists:

Using fs.stat() to check for the existence of a file before calling fs.open(), fs.readFile() or fs.writeFile() is not recommended. Instead, user code should open/read/write the file directly and handle the error raised if the file is not available.

To check if a file exists without manipulating it afterwards, fs.access() is recommended.

If you don't need to read or write the file you should use fs.access, the simple and asynchronous way is:

try {
 fs.accessSync(path)
 // the file exists
}catch(e){
 // the file doesn't exists
}
David Lemon
  • 1,560
  • 10
  • 21