existsSync

import { existsSync } from "jsr:@ende/fs";

function

function existsSync(path: string | URL, options?: ExistsOptions): boolean

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.

path: string | URL

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

options?: ExistsOptions

Additional options for the check.

Returns

true if the path exists, false otherwise.

Example: Recommended method

// Notice no use of exists
try {
  Ende.removeSync("./foo", { recursive: true });
} catch (error) {
  if (!(error instanceof Ende.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 "@ende/fs/exists";

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

Example: Check if a path is readable

Requires --allow-sys permissions in some cases.

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

existsSync("./readable", { isReadable: true }); // true
existsSync("./not_readable", { isReadable: true }); // false

Example: Check if a path is a directory

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

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

Example: Check if a path is a file

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

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

Example: Check if a path is a readable directory

Requires --allow-sys permissions in some cases.

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

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

Example: Check if a path is a readable file

Requires --allow-sys permissions in some cases.

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

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

See also

Source