Check if a path is a file in Node.js using Promises. Returns false on error, also serving as an existence check.
This kind of helper is useful when I do not want to wrap fs.stat() with try-catch every time. It fits optional config files, user-provided paths, and cleanup flows that should only run when a regular file is actually present.
import fs from "node:fs/promises";
function isFile(path: string): Promise<boolean> { return fs .stat(path) .then((stats) => stats.isFile()) .catch(() => false);}Where It Helps
Use it when the condition is simply “the path exists and it must be a regular file, not a directory.” The call site stays small because it does not have to repeat exception handling on every branch.
Why This Shape Is Handy
The helper reduces the result of fs.stat() to stats.isFile() and collapses every exception into false. If both missing paths and non-file targets should be treated as unusable, this small API is usually enough.
Notes
This version does not surface what kind of error occurred. That means it does not distinguish a missing file from a permission error. If that difference matters, inspect the error code in catch instead of swallowing everything. If I need to check a symbolic link itself rather than its target, lstat() is the better fit.
hsb.horse