'Electron + NodeJs: fs.statSync() file size is always 0

I'm developing a Electron app and i need to access a file's size. I'm using fs.statSync() to accomplish this. My issue is that the fs.statSync() stats.size is always 0 (zero).

Here's how i'm getting the file size:

var stats = fs.statSync(filePath);
var fileSize = stats.size; //file size is always 0
console.log("fileSize: " + fileSize);

The Stats detailed object data:

Stats {dev: 1584160373, mode: 33206, nlink: 1, uid: 0, gid: 0…}
atime: Wed Oct 18 2017 23:39:53 GMT+0100 (GMT Summer Time)
birthtime: Wed Oct 18 2017 23:10:50 GMT+0100 (GMT Summer Time)
blksize: undefined
blocks: undefined
ctime: Thu Oct 19 2017 00:35:33 GMT+0100 (GMT Summer Time)
dev: 1584160373
gid: 0
ino: 8444249301430590
mode: 33206
mtime: Thu Oct 19 2017 00:35:33 GMT+0100 (GMT Summer Time)
nlink: 1
rdev: 0
size: 0
uid: 0

What am i doing wrong here? Btw, i'm using Windows 10 x64.

UPDATE: I solved my issue. The problem wasn't with fs.statSync(), but instead with the files (they were all 0 in size). Rookie mistake, my bad... ¯_(ツ)_/¯



Solution 1:[1]

A folder has no size, the files in the folder do. You could walk recursive through all files and subfolders to collect each filesize. Or use a package like 'get-folder-size' for that.

If you are only running on windows or linux, you could try to get the folder size from a native program like du -ksh path/to/folder which will be faster to some degree.

Solution 2:[2]

You may want to determine if any other functions are taking up reading and writing of the file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hans Koch
Solution 2