Methods
exists

exists

Important!

Unlike most other methods exists is not backwards compatible with Node's depr ecated exists (opens in a new tab) or existsSync (opens in a new tab). Instead universal-fs has it's own more intuitive exists method. The primary differences are:

  • It is not synchronous and must be called using async/await
  • It does not support a callback API
  • You can abort the operation by passing an AbortSignal

For the full reasoning behind this please see Why is exists not backwards compatible with Node fs? (opens in a new tab)

How does exists work?

A modern implementation of the Node fs exists (opens in a new tab) API. Returns true if the path exists, false otherwise in promise form.

Usage

import {exists} from "universal-fs";
 
const hasFoundFile = await exists("/path/to/file.txt");

You can also abort an outgoing request using an AbortSignal. If a request is aborted the promise returned is rejected with an AbortError:

import {exists} from "universal-fs";
 
const controller = new AbortController();
const {signal} = controller;
 
const hasFoundFile = await exists("/path/to/file.txt", {signal});
 
controller.abort();

Additional details

  • since universal-fs v1.3.0
  • return A promise that resolves to true if found otherwise false