Function exists

  • Asynchronously 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 Promise<boolean>

    A promise that resolves with true if the path exists, false otherwise.

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

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

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

    Requires --allow-sys permissions in some cases.

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

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

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

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

    Requires --allow-sys permissions in some cases.

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

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

    Requires --allow-sys permissions in some cases.

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

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