3

I know exists is deprecated in fs but it is able in fs-extra.

I used both fsExtra.pathExists and fsExtra.exists.

But I could not find the difference.

They performe same.

TGrif
  • 5,725
  • 9
  • 31
  • 52
Kuru
  • 1,417
  • 2
  • 13
  • 20

1 Answers1

4

There is not much difference between these two methods.

fs-extra is a superset of fs, inheriting all its methods, so fsExtra.exists is the same as fs.exists. And as you said it's deprecated.

The difference between exists() and pathExists() lies in the signature of the functions.

Like fs.exists, but with a normal callback signature (err, exists).

Internally, fs native module use a try catch block, while fs-extra use a Promise style. And they both use fs.access() method to determine if the specified file is accessible.

And yes, they both have the same use.

TGrif
  • 5,725
  • 9
  • 31
  • 52
  • Thank you so much. But I am sorry, I am Japanese and not good at English. What is the word signature means? You mean just a difference of name of functions? – Kuru Jun 10 '19 at 01:49
  • fsExtra.exists and fsExtra.pathExists both return Promise. So, I thought it's same. – Kuru Jun 10 '19 at 01:50
  • Sorry now I understand what you mean. I will put edit on question to confirm. – Kuru Jun 10 '19 at 01:54
  • Yes basically, the callback version of `pathExists()` add the `err` param while `exists()` doesn't. As said in documentation, "The parameters for this callback are not consistent with other Node.js callbacks". That's why it is deprecated. – TGrif Jun 10 '19 at 09:22
  • Thank you so much. But I can't understand why fs.exists callback is not consistent with other Node.js callbacks and fsExtra.pathExists callback is consistent with other Node.js callbacks. There are no difference I think. – Kuru Jun 11 '19 at 03:56
  • In Node.js, the callback function usually takes 2 arguments: `(err, data)`. `fs.exists` has only one argument. As the [doc](https://nodejs.org/api/fs.html#fs_fs_exists_path_callback) says: "Normally, the first parameter to a Node.js callback is an err parameter, optionally followed by other parameters. The fs.exists() callback has only one boolean parameter." – TGrif Jun 11 '19 at 10:17
  • Hmm thank you so much you are very knowledgeable. I understand. – Kuru Jun 12 '19 at 05:41