1

Executing the below function test () with Node.js v8.11.2 on my Windows-10 prints out this:

DOES NOT EXIST ? C:\pagefile.sys
DOES NOT EXIST ? C:\hiberfil.sys
DOES NOT EXIST ? C:\swapfile.sys
DOES NOT EXIST ? C:\System Volume Information

These all seem to be invisible system files yes.

But they obviously exist. So why does Node.js tell me they don't exist? Are there other cases where file or folder is listed by readdirSync() yet existsSync() returns false for them?

Is this a feature or bug?

function test ()
{ let Fs   = require("fs");
  let Path = require("path");
  let path = "C:/";

  let ents = Fs.readdirSync(path) ;
  let dirs = ents.map
  ( e =>
        { let absPath = Path.join (path, e);
          if (! Fs.existsSync(absPath))
          { console.log ("DOES NOT EXIST ? " + absPath)
          } else
          { console.log ("DOES EXIST: " + absPath)
          }
        }
  );
}
Panu Logic
  • 2,193
  • 1
  • 17
  • 21
  • FWIW `exists` is now deprecrated, if you don't intend on manipulating the file then you should be using [access](https://nodejs.org/api/fs.html#fs_fs_accesssync_path_mode). – James May 29 '18 at 21:03
  • BTW. There is a related question on SO https://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js with lots of info on "exists()" including that "exists()" is deprecated but "existsSync()" is not. But nothing as far as I can see about the question of what exists() does or should do with invisible files. – Panu Logic May 30 '18 at 05:07
  • Hmm odd that they’d deprecate one version and not both, well thing is `existsSync` will never really give you much insight into *why* a file isn’t visible based on its implementation (it suppresses errors). You may gain more insight using `access`. – James May 30 '18 at 07:27

0 Answers0