Function existsSync
- exists
Sync (path, options?): boolean Parameters
- path: string | URL
The path to the file or directory, as a string or URL.
Optionaloptions: ExistsOptionsAdditional options for the check.
Returns boolean
trueif the path exists,falseotherwise.See
- https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use for more information on the time-of-check to time-of-use bug.
Requires
--allow-readpermissions, and in some cases,--allow-syspermissions ifoptions.isReadableistrue.- https://docs.deno.com/runtime/manual/basics/permissions#file-system-access for more information on Deno's permissions system.
Example: 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 note for details.Example: Basic usage
import { existsSync } from "@std/fs/exists";
existsSync("./exists"); // true
existsSync("./does_not_exist"); // falseExample: Check if a path is readable
Requires
--allow-syspermissions in some cases.import { existsSync } from "@std/fs/exists";
existsSync("./readable", { isReadable: true }); // true
existsSync("./not_readable", { isReadable: true }); // falseExample: Check if a path is a directory
import { existsSync } from "@std/fs/exists";
existsSync("./directory", { isDirectory: true }); // true
existsSync("./file", { isDirectory: true }); // falseExample: Check if a path is a file
import { existsSync } from "@std/fs/exists";
existsSync("./file", { isFile: true }); // true
existsSync("./directory", { isFile: true }); // falseExample: Check if a path is a readable directory
Requires
--allow-syspermissions 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- path: string | URL
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.