[Node.js] Check whether a file or directory exists

Created on November 30, 2026

You can call existsSync() from the node:fs module to check whether a path exists. This example checks for reports in the current working directory.

const fs = require("node:fs");

if (fs.existsSync("./reports")) {
  console.log("The reports path exists");
}

The function returns true for either a file or a directory at that path. If the path exists, the example prints:

The reports path exists
Node.js