import { exists } from "https://deno.land/std@0.222.1/fs/mod.ts";
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.
Examples
Recommended method
Recommended method
// Notice no use of exists
try {
await Deno.remove("./foo", { recursive: true });
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
// Do nothing...
}
Notice that exists()
is not used in the above example. Doing so avoids a
possible race condition. See the above section for details.
Basic usage
Basic usage
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./exists"); // true
await exists("./does_not_exist"); // false
Check if a path is readable
Check if a path is readable
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./readable", { isReadable: true }); // true
await exists("./not_readable", { isReadable: true }); // false
Check if a path is a directory
Check if a path is a directory
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./directory", { isDirectory: true }); // true
await exists("./file", { isDirectory: true }); // false
Check if a path is a file
Check if a path is a file
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./file", { isFile: true }); // true
await exists("./directory", { isFile: true }); // false
Check if a path is a readable directory
Check if a path is a readable directory
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./readable_directory", { isReadable: true, isDirectory: true }); // true
await exists("./not_readable_directory", { isReadable: true, isDirectory: true }); // false
Check if a path is a readable file
Check if a path is a readable file
import { exists } from "https://deno.land/std@0.222.1/fs/exists.ts";
await exists("./readable_file", { isReadable: true, isFile: true }); // true
await exists("./not_readable_file", { isReadable: true, isFile: true }); // false
Parameters
The path to the file or directory, as a string or URL.
Additional options for the check.