G
G
gcj2018-11-05 16:56:08
Node.js
gcj, 2018-11-05 16:56:08

How to check if a file is empty?

Greetings, there is such an interesting module for node.js called fs .
The task is this: I need to check the existence of the contents of the file and, under certain conditions, do something with this file.
Well, i.e. IF the file is empty, then delete it, for example.
Either I'm not careful, or there are problems with the perception of some methods of this module. Tell me, please, how to perform such a check? If there are analogues of more interesting analogues, then advise.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilya Gerasimov, 2018-11-05
@gcj

const stat = fs.statSync('./path/to/file');
console.log(stat.size);

0
0xD34F, 2018-11-05
@0xD34F

fs.stat(filename, function(err, stats) {
  if (stats.size) {
    console.log('not empty');
  } else {
    console.log('empty');
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question