How can I synchronously check, using node.js, if a file or directory exists?
Node.js – Check synchronously if file/directory exists in Node.js
fsnode.js
Related Topic
- Javascript – How to get a list of the names of all files present in a directory in Node.js
- Node.js – How to get the path to the current script with Node.js
- Javascript – How to pass command line arguments to a Node.js program
- Javascript – How to decide when to use Node.js
- Javascript – the purpose of Node.js module.exports and how do you use it
- Node.js – How to update NodeJS and NPM to the next versions
- Node.js – How to get GET (query string) variables in Express.js on Node.js
- Node.js – Find the version of an installed npm package
Best Answer
The answer to this question has changed over the years. The current answer is here at the top, followed by the various answers over the years in chronological order:
Current Answer
You can use
fs.existsSync()
:It was deprecated for several years, but no longer is. From the docs:
You've specifically asked for a synchronous check, but if you can use an asynchronous check instead (usually best with I/O), use
fs.promises.access
if you're usingasync
functions orfs.access
(sinceexists
is deprecated) if not:In an
async
function:Or with a callback:
Historical Answers
Here are the historical answers in chronological order:
(
stat
/statSync
orlstat
/lstatSync
)(
exists
/existsSync
)(Noting impending deprecation of
exists
/existsSync
, so we're probably back tostat
/statSync
orlstat
/lstatSync
)(There's also
fs.access(path, fs.F_OK, function(){})
/fs.accessSync(path, fs.F_OK)
, but note that if the file/directory doesn't exist, it's an error; docs forfs.stat
recommend usingfs.access
if you need to check for existence without opening)fs.exists()
is still deprecated butfs.existsSync()
is no longer deprecated. So you can safely use it now.Original answer from 2010:
You can use
statSync
orlstatSync
(docs link), which give you anfs.Stats
object. In general, if a synchronous version of a function is available, it will have the same name as the async version withSync
at the end. SostatSync
is the synchronous version ofstat
;lstatSync
is the synchronous version oflstat
, etc.lstatSync
tells you both whether something exists, and if so, whether it's a file or a directory (or in some file systems, a symbolic link, block device, character device, etc.), e.g. if you need to know if it exists and is a directory:...and similarly, if it's a file, there's
isFile
; if it's a block device, there'sisBlockDevice
, etc., etc. Note thetry/catch
; it throws an error if the entry doesn't exist at all.If you don't care what the entry is and only want to know whether it exists, you can usepath.existsSync
(or with latest,fs.existsSync
) as noted by user618408:It doesn't require atry/catch
but gives you no information about what the thing is, just that it's there.path.existsSync
was deprecated long ago.Side note: You've expressly asked how to check synchronously, so I've used the
xyzSync
versions of the functions above. But wherever possible, with I/O, it really is best to avoid synchronous calls. Calls into the I/O subsystem take significant time from a CPU's point of view. Note how easy it is to calllstat
rather thanlstatSync
:But if you need the synchronous version, it's there.
Update September 2012
The below answer from a couple of years ago is now a bit out of date. The current way is to use
fs.existsSync
to do a synchronous check for file/directory existence (or of coursefs.exists
for an asynchronous check), rather than thepath
versions below.Example:
Update February 2015
And here we are in 2015 and the Node docs now say that
fs.existsSync
(andfs.exists
) "will be deprecated". (Because the Node folks think it's dumb to check whether something exists before opening it, which it is; but that's not the only reason for checking whether something exists!)So we're probably back to the various
stat
methods... Until/unless this changes yet again, of course.Update December 2015
Don't know how long it's been there, but there's also
fs.access(path, fs.F_OK, ...)
/fs.accessSync(path, fs.F_OK)
. And at least as of October 2016, thefs.stat
documentation recommends usingfs.access
to do existence checks ("To check if a file exists without manipulating it afterwards,fs.access()
is recommended."). But note that the access not being available is considered an error, so this would probably be best if you're expecting the file to be accessible:Update December 2016
You can use
fs.existsSync()
:It was deprecated for several years, but no longer is. From the docs: