import { existsSync } from "https://dotland.deno.dev/std@0.224.0/fs/exists.ts";
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.
Examples
Recommended method
Recommended method
// Notice no use of exists
try {
Deno.removeSync("./foo", { recursive: true });
} catch (error) {
if (!(error instanceof Deno.errors.NotFound)) {
throw error;
}
// Do nothing...
}
Notice that existsSync()
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 { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
existsSync("./exists"); // true
existsSync("./does_not_exist"); // false
Check if a path is readable
Check if a path is readable
import { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
existsSync("./readable", { isReadable: true }); // true
existsSync("./not_readable", { isReadable: true }); // false
Check if a path is a directory
Check if a path is a directory
import { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
existsSync("./directory", { isDirectory: true }); // true
existsSync("./file", { isDirectory: true }); // false
Check if a path is a file
Check if a path is a file
import { existsSync } from "https://deno.land/std@0.224.0/fs/exists.ts";
existsSync("./file", { isFile: true }); // true
existsSync("./directory", { isFile: true }); // false