0

I'm writing a plugin for some tool and the API is JavaScript. I need to know whether a file exists or not, and I'd like to avoid operating system calls in order to abstract from it, but using the JavaScript language instead, if possible.

Is it possible to know if a file exists locally in JavaScript, given a path?

Luis
  • 2,833
  • 3
  • 27
  • 41

1 Answers1

1

JavaScript has no native file handling functions whatsoever. Any access to the file system needs to be via an API provided by the host environment.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • This is kinda wrong, depends on the enviroment. Javascript running in the browser usually has no access to the local or remote filesystem, true. – Hless Oct 27 '14 at 12:32
  • 2
    @Hless — The fact that JavaScript has no native file handling functions does not depend on the environment. What depends on the environment is what APIs are provided by the environment. JS in the browser has access to the file system through the [File API](http://www.w3.org/TR/FileAPI/) (which is provided by the host environment and isn't defined by JavaScript). JS in Node has access to the file system through [the File System mode](http://nodejs.org/api/fs.html) which, again, is provided by the host environment and isn't native JavaScript. – Quentin Oct 27 '14 at 12:34
  • That is like saying that XMLHTTP request are also not part of JavaScript and that JavaScript has no HTTP handling functions at all. Which is in essence true, but I think it makes little sense to OP. – Hless Oct 27 '14 at 12:37
  • XMLHttpRequest isn't part of JavaScript, and it isn't usually available outside of browsers. The question implies that the OP isn't working in a browser anyway, so it doesn't make sense to assume they are. – Quentin Oct 27 '14 at 12:45
  • I know. Just saying the answer can be confusing depending on how you interpret it. Not everyone has this knowledge. Perhaps the 'wrong' statement was too bold, that was misinterpretation on my side. – Hless Oct 27 '14 at 12:47
  • It seems that you're right. For security reasons, JavaScript does has access to the local FS. Thanks for your feedback. – Luis Oct 27 '14 at 13:53
  • As I pointed out, many host environments provide a means to access the filesystem from JS. – Quentin Oct 27 '14 at 14:02