Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
37
rated 0 times [  42] [ 5]  / answers: 1 / hits: 23372  / 9 Years ago, wed, may 20, 2015, 12:00:00

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


More From » node.js

 Answers
18

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.



[#66526] Tuesday, May 19, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;