Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/fs/mod.ts>exists

The Deno Standard Library
Go to Latest
function exists
import { exists } from "https://deno.land/std@0.223.0/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

// 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

import { exists } from "https://deno.land/std@0.223.0/fs/exists.ts";

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

Check if a path is readable

import { exists } from "https://deno.land/std@0.223.0/fs/exists.ts";

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

Check if a path is a directory

import { exists } from "https://deno.land/std@0.223.0/fs/exists.ts";

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

Check if a path is a file

import { exists } from "https://deno.land/std@0.223.0/fs/exists.ts";

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

Check if a path is a readable directory

import { exists } from "https://deno.land/std@0.223.0/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

import { exists } from "https://deno.land/std@0.223.0/fs/exists.ts";

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

Parameters

path: string | URL

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

optional
options: ExistsOptions

Additional options for the check.

Returns

Promise<boolean>

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