The parameters for this callback are not consistent with other Node.jscallbacks. Normally, the first parameter to a Node.js callback is an errparameter, optionally followed by other parameters. The fs.exists() callback
has only one boolean parameter. This is one reason fs.access() is recommended
instead of fs.exists().
Using fs.exists() to check for the existence of a file before callingfs.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 does not exist.
The "not recommended" examples above check for existence and then use the
file; the "recommended" examples are better because they use the file directly
and handle the error, if any.
In general, check for the existence of a file only if the file won’t be
used directly, for example when its existence is a signal from another
process.
Test whether or not the given path exists by checking with the file system. Then call the
callback
argument with either true or false:The parameters for this callback are not consistent with other Node.js callbacks. Normally, the first parameter to a Node.js callback is an
err
parameter, optionally followed by other parameters. Thefs.exists()
callback has only one boolean parameter. This is one reasonfs.access()
is recommended instead offs.exists()
.Using
fs.exists()
to check for the existence of a file before callingfs.open()
,fs.readFile()
orfs.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 does not exist.write (NOT RECOMMENDED)
write (RECOMMENDED)
read (NOT RECOMMENDED)
read (RECOMMENDED)
The "not recommended" examples above check for existence and then use the file; the "recommended" examples are better because they use the file directly and handle the error, if any.
In general, check for the existence of a file only if the file won’t be used directly, for example when its existence is a signal from another process.
Since
v0.0.2
Deprecated
Since v1.0.0 - Use stat or access instead.