Function existsSync

  • Synchronously test whether or not the given path exists by checking with the file system.

    Note: Do not use this function if performing a check before another operation on that file. Doing so creates a race condition. Instead, perform the actual file operation directly. This function is not recommended for this use case. See the recommended method below.

    Parameters

    • path: string | URL

      The path to the file or directory, as a string or URL.

    • Optionaloptions: ExistsOptions

      Additional options for the check.

    Returns boolean

    true if the path exists, false otherwise.

    Requires --allow-read permissions, and in some cases, --allow-sys permissions if options.isReadable is true.

    import { existsSync } from "@std/fs/exists";

    existsSync("./exists"); // true
    existsSync("./does_not_exist"); // false

    Requires --allow-sys permissions in some cases.

    import { existsSync } from "@std/fs/exists";

    existsSync("./readable", { isReadable: true }); // true
    existsSync("./not_readable", { isReadable: true }); // false
    import { existsSync } from "@std/fs/exists";

    existsSync("./directory", { isDirectory: true }); // true
    existsSync("./file", { isDirectory: true }); // false
    import { existsSync } from "@std/fs/exists";

    existsSync("./file", { isFile: true }); // true
    existsSync("./directory", { isFile: true }); // false

    Requires --allow-sys permissions in some cases.

    import { existsSync } from "@std/fs/exists";

    existsSync("./readable_directory", { isReadable: true, isDirectory: true }); // true
    existsSync("./not_readable_directory", { isReadable: true, isDirectory: true }); // false

    Requires --allow-sys permissions in some cases.

    import { existsSync } from "@std/fs/exists";

    existsSync("./readable_file", { isReadable: true, isFile: true }); // true
    existsSync("./not_readable_file", { isReadable: true, isFile: true }); // false