Node.js · Junior · Question 54 of 75
How do you check whether a file exists before reading it?
- 1Call
fs.exists()first, then read the file - 2Read
process.files - ✓Correct answer: Usually you don't: just read it and handle the
ENOENTerror
Explanation
Checking first creates a race condition: the file can be deleted between the check and the read. Handling the error is simpler and correct. fs.exists is deprecated.