JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 156374
  • Score
    100M100P100Q155687F
  • License MIT

Check if a file or directory exists in a given path.

Package Exports

  • is-there

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (is-there) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

is-there

is-there Donate now

Check if a file or directory exists in a given path.

Why? fs.exists already does the job!

Because fs.exists and fs.existsSync will be deprecated and I still like and need them!

fs.exists() is an anachronism and exists only for historical reasons. There should almost never be a reason to use it in your own code.

In particular, checking if a file exists before opening it is an anti-pattern that leaves you vulnerable to race conditions: another process may remove the file between the calls to fs.exists() and fs.open(). Just open the file and handle the error when it's not there.

fs.exists() will be deprecated.

(Source, emphasis added)

Installation

$ npm i is-there

Example

// Dependencies
var IsThere = require("is-there");

// Paths to test
var paths = [
    // exist
    "dir"
  , "dir/another"
  , "dir/another/file"
  , "dir/file"
  , "file"
  , "file.ext"
    // don't exist
  , "foo"
  , "foo/bar"
  , "foo.bar"
  , "foo/bar.foo"
].map(function (c) {
    return __dirname + "/contents/" + c;
});

// Sync
console.log("> Testing sync method.");
paths.forEach(function (c) {
    console.log("> %s %s", c, IsThere(c) ? "exists" : "doesn't exist");
});

console.log("> Testing async method.");
function doSeq(i) {
    i = i || 0;
    var cPath = paths[i];
    if (!cPath) { return; }
    IsThere(cPath, function (exists) {
        console.log("> %s %s", cPath, exists ? "exists" : "doesn't exist");
        doSeq(i + 1);
    });
}

doSeq();

Documentation

IsThere(path, callback)

Checks if a file or directory exists on given path.

Params

  • String path: The path to the file or directory.
  • Function callback: The callback function called with a boolean value representing if the file or directory exists. If this parameter is not a function, the function will run the synchronously and return the value.

Return

  • IsThere|Boolean The IsThere function if the callback parameter was provided, otherwise a boolean value indicating if the file/directory exists or not.

How to contribute

Have an idea? Found a bug? See how to contribute.

License

KINDLY © Ionică Bizău–The LICENSE file contains a copy of the license.