Node.js Reference ยท fs and fs/promises
Node.js fs.existsSync() method
Reference for fs.existsSync() in Node.js: check whether a path exists, why it never throws, the deprecated fs.exists(), and race conditions to avoid.
fs.existsSync(path) returns true if something exists at path (a file, directory, or anything else) and false otherwise. It does not throw for missing paths or permission problems; those simply give false.
It is the one fs check that is not deprecated in its synchronous form, and it is handy in scripts and start-up code ("does the config file exist?"). In servers and for anything where another process may change the file system, prefer to just try the operation and handle the error, because a check followed by an action is a race.
import { existsSync } from 'node:fs'; if (existsSync('.env')) { console.log('found a .env file'); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
fs.existsSync(path): boolean // path: string | Buffer | URL // deprecated callback form: fs.exists(path, callback) // callback: (exists: boolean) => void
| Name | Description |
|---|---|
path |
Path to test. Relative paths resolve against the current working directory. |
Returns: true if the path exists, false if not (or if it cannot be checked).
Errors: none are thrown. Failures to look the path up give false. Passing a value that is not a string, Buffer or URL (such as undefined) also returns false, but current Node.js versions print a deprecation warning (DEP0187) for it and a future version may throw, so validate your input.
Usage notes
- No promise version.
fs/promiseshas noexists(). Useaccess()orstat()and catch the error, or write a tiny helper:const exists = (p) => access(p).then(() => true, () => false). fs.exists()is deprecated. Its callback receives a single boolean, which breaks the usual(err, value)convention and does not work withutil.promisify()out of the box.- Race conditions (TOCTOU). Between the check and your next call, another process can create or delete the path. Opening with
'wx'to create exclusively, or catchingENOENTwhen reading, is correct; "check then act" is not. falsedoes not always mean "missing". A path in a directory you may not search (no execute permission) or an over-long path also yieldsfalse.- Symlinks are followed. A link whose target is gone reports
false. Uselstat()to test the link itself. - Does not tell the type. For "is it a file or a directory" use
fs.stat()and the methods offs.Stats. - Does not tell permissions. See
fs.access().