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
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.
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 callingfs.open(), fs.readFile()
orfs.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With