fs.exists, fs.existsSync - why are they deprecated?

There is no need to use fs.stat(), because fs.existsSync() has not been deprecated.

https://nodejs.org/api/fs.html#fs_fs_existssync_path

fs.existsSync(path)

Added in: v0.1.21 path | Synchronous version of fs.exists(). Returns true if the file exists, false otherwise.

Note that fs.exists() is deprecated, but fs.existsSync() is not. (The callback >parameter to fs.exists() accepts parameters that are inconsistent with other >Node.js callbacks. fs.existsSync() does not use a callback.)


Because of the second paragraph you quoted.

There is no point in checking whether a file exists, because it could always be deleted right after the check.


Being deprecated because it's an anti-pattern according to some. I.e. it's not safe to trust exists() and then doing something with the file because the file can be removed in between the exists-call and the doing-something-call.

I agree in the above case. But to me, there is more use of exists(). I place empty dummy files in my temp- and cache directories. When I perform dangerous operations, such as removing old files from the cache dir I look for my dummy file to ensure that I'm not operating in the wrong directory. I.e. I just need to confirm that the file is there. Exists suits the bill perfectly for this, but I guess I'll switch to using stat() instead.


I think it because it's redundant. You can check if a file exists by trying to open it. It will give you ENOENT if it doesn't exist:

> fs.open('foo', 'r', function(err, fd) {
    ... console.log(err, fd);
    ... 
})
undefined
> { [Error: ENOENT, open 'foo'] errno: 34, code: 'ENOENT', path: 'foo' } undefined

Tags:

Node.Js