How to check if a file exists in Node (async)?


async function fileExists(path: string): Promise<boolean> {
  return access(path)
    .then(() => true)
    .catch(() => false);


You used to be able to use fs.exists() but (for some strange reason) it has been deprecated as well as all it’s cousins.

You have a few options including using fs.stat or using fs.access. Likely the cleanest is to use fs.access to see if the current user has access to the given path and if they do, return true.

Since access throws an error if it cannot access the given path, we use .catch to return false since we will assume if there is a file that we cannot access, it effectively does not exist (at least from the perspective of the current user).