Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if file exist in Gulp

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

like image 226
Caio Kawasaki Avatar asked May 20 '15 11:05

Caio Kawasaki


People also ask

How do you check if a path exists in JS?

The simplest way to check if a certain directory exists in Node. js is by using the fs. existsSync() method. The existsSync() method returns true if the path exists, false otherwise.


1 Answers

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.

like image 129
Alexander Avatar answered Sep 21 '22 11:09

Alexander