Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Update fs.markdown to account for correct uses of fs.exists() #7944

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions doc/api/fs.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -648,13 +648,10 @@ 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.exists()` should not be used to check if a file exists before calling
`fs.open()`. This introduces a race condition, since other processes may change
the file's state between the two calls. Instead, user code should call `fs.open()`
directly, and handle the error if the file is non-existent.

## fs.existsSync(path)

Expand Down