Skip to content

Commit d97ea06

Browse files
committed
doc: add warning to fs.exists() documentation
Warn against the open-if-exists anti-pattern, it's susceptible to race conditions.
1 parent b7f36e1 commit d97ea06

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

doc/api/fs.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,13 @@ Then call the `callback` argument with either true or false. Example:
595595
util.debug(exists ? "it's there" : "no passwd!");
596596
});
597597

598+
`fs.exists()` is an anachronism and exists only for historical reasons.
599+
There should almost never be a reason to use it in your own code.
600+
601+
In particular, checking if a file exists before opening it is an anti-pattern
602+
that leaves you vulnerable to race conditions: another process may remove the
603+
file between the calls to `fs.exists()` and `fs.open()`. Just open the file
604+
and handle the error when it's not there.
598605

599606
## fs.existsSync(path)
600607

0 commit comments

Comments
 (0)