Instructions for the fs.exists method in node.js

  • 2020-05-07 19:13:07
  • OfStack

method description:

Test for the existence of a file under a path.

The callback function contains one parameter exists, true means the file exists, otherwise false.


fs.exists(path, callback)

Since this method belongs to fs module, fs module (var fs= require(" fs ") needs to be introduced before use.)

receive parameter:

The file path to be detected by path

callback callback


fs.exists('/etc/passwd', function (exists) {
  util.debug(exists ? "it's there" : "no passwd!");

source code:

fs.exists = function(path, callback) {
  if (!nullCheck(path, cb)) return;
  binding.stat(pathModule._makeLong(path), cb);
  function cb(err, stats) {
    if (callback) callback(err ? false : true);

Related articles: