I know that this question is somewhat old, but recently I found myself looking for a clear and short answer on how to do it, if it is useful to someone, well, and if it has not only consumed a few bytes.
I must clarify that I do not I am an expert in anything, but I like to learn and this is what I learned looking for a solution to this:
*/
First declare the needs of a Child Process and [execSync()][1]
"the method will not return until the child process has fully closed"
*/
This script is a Synchronous operation
//Declares the required module
const execSync = require('child_process').execSync;
//Declare the directory or file path
const target = "Absolute path to dir or file";
/*
Declare a variable or constant to store the data returned,
parse data to Number and multiplying by 1024 to get total
bytes
*/
const size = parseInt(execSync(`du '${target}'`)) * 1024;
//Finally return or send to console, the variable or constant used for store data
return size;
With exec or execSync can execute files, or commands, in Unix system when executes du 'some path' in a terminal, gets the disk utilization of the file or directory, and the absolute pat again,therefore it is necessary to make a parse to integer of the result, the execSync gets a buffer as result.
I use a Template String as parameter to avoid writing more lines of code since you don't have to deal with whitespace issues in the string path, this method supports these whitespace.
//If executed in a terminal
du 'path to file or directory including white spaces in names'
// returns something like
125485 path to file or directory including white spaces in names
All about du command for Unix
I don't speak English natively so I use a translator as an interpreter, my apologies for language errors.
All about du equivalent for Windows
du
is a linux program, maybe on windows 10 it is available in some fashion, but it would also be available on windows if you had something like cygwin installed...