The reliable way to check whether a file exists in Node.js is to call access() from node:fs/promises inside try and catch, or simply to try the operation you want and handle the ENOENT error. Plain fs.exists() is deprecated, and checking first and acting later has a race condition.
This guide shows the modern access() check, the synchronous existsSync() helper, why "check then use" is risky, and the better pattern of handling the error directly. Examples use ES modules on current Node.js LTS releases (22 and 24).
access() tests whether the process can reach a path. It resolves if the path is accessible and rejects with an error if not. Wrap it in a small helper:
By default access() checks plain existence. You can pass a mode from fs.constants to test permissions as well:
A catch-all catch treats every error as "does not exist", including a permission error. If the difference matters, look at err.code: ENOENT means missing, while EACCES means it exists but you cannot reach it.
existsSync() is the synchronous check. It returns a boolean and never throws:
It is not deprecated, unlike the old callback function fs.exists(), which you should not use. Because existsSync blocks, keep it to start-up code and scripts. Also note that it returns true for folders as well as files.
Existence alone does not tell you the type. Use stat() when you need to know:
Notice that this version rethrows errors other than ENOENT, so a real problem such as a permission failure is not hidden. stat() follows symbolic links; use lstat() if you want to inspect the link itself.
A common pattern looks like this:
Between the access call and the readFile call, another process can delete or create the file. This gap is a time-of-check to time-of-use problem. In security-sensitive code it can even be exploited. The Node.js documentation advises against using access() before opening a file for this reason.
The fix is to skip the check and handle the failure of the real operation:
The same idea applies to creating files. To create a file only when it does not exist, use the exclusive flag, which is atomic:
| Goal | Best approach |
|---|---|
| Read a file that may be missing | readFile and handle ENOENT |
| Create a file only if absent | writeFile with the "wx" flag |
| Need file versus folder | stat() and isFile() |
| Quick check at start-up | existsSync() |
| Show or report only | access() in a helper |
Yes, the callback function fs.exists() is deprecated. fs.existsSync() is still supported, and fs.promises.access() is the modern asynchronous option.
Because the file state can change between the check and the open. Handling the error from the open call is both simpler and safe.
Yes. It reports that the path exists, whatever its type. Use stat() to tell files from folders.
Use access() or stat() from node:fs/promises with await.
Use access() for a quick asynchronous existence check and existsSync() for start-up scripts, but prefer handling ENOENT from the real operation to avoid race conditions. Use stat() when you need the type, and the "wx" flag to create a file only if it is new.