Skip to content

Commit

Permalink
doc: add warning to fs.exists() documentation
Browse files Browse the repository at this point in the history
Warn against the open-if-exists anti-pattern, it's susceptible to
race conditions.
  • Loading branch information
bnoordhuis committed Oct 5, 2013
1 parent b7f36e1 commit d97ea06
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,13 @@ Then call the `callback` argument with either true or false. Example:
util.debug(exists ? "it's there" : "no passwd!");
});

`fs.exists()` is an anachronism and exists only for historical reasons.
There should almost never be a reason to use it in your own code.

In particular, checking if a file exists before opening it is an anti-pattern
that leaves you vulnerable to race conditions: another process may remove the
file between the calls to `fs.exists()` and `fs.open()`. Just open the file
and handle the error when it's not there.

## fs.existsSync(path)

Expand Down

0 comments on commit d97ea06

Please sign in to comment.