How can I synchronously check, using node.js, if a file or directory exists?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.accessif you’re usingasyncfunctions orfs.access(sinceexistsis deprecated) if not:In an
asyncfunction:Or with a callback:
Historical Answers
Here are the historical answers in chronological order:
(
stat/statSyncorlstat/lstatSync)(
exists/existsSync)(Noting impending deprecation of
exists/existsSync, so we’re probably back tostat/statSyncorlstat/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.statrecommend usingfs.accessif 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
statSyncorlstatSync(docs link), which give you anfs.Statsobject. In general, if a synchronous version of a function is available, it will have the same name as the async version withSyncat the end. SostatSyncis the synchronous version ofstat;lstatSyncis the synchronous version oflstat, etc.lstatSynctells 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 a
try/catchbut gives you no information about what the thing is, just that it’s there.path.existsSyncwas deprecated long ago.Side note: You’ve expressly asked how to check synchronously, so I’ve used the
xyzSyncversions 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 calllstatrather 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.existsSyncto do a synchronous check for file/directory existence (or of coursefs.existsfor an asynchronous check), rather than thepathversions 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
statmethods… 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.statdocumentation recommends usingfs.accessto 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: