This module implements platform specific bindings to obtain disk usage information on Windows and POSIX platforms. Windows support is backed by GetDiskFreeSpaceEx and POSIX is implemented with statvfs.
Install with npm:
$ npm install diskusageThe module exposes two functions. check takes a path/mount point as the first argument and a callback as the second. The callback takes two arguments err and info. err will be an Error if something went wrong. info contains three members: available, free and total in bytes.
If no callback is supplied check will instead return a Promise<DiskUsage> that you can await.
available: Disk space available to the current user (i.e. Linux reserves 5% for root)free: Disk space physically freetotal: Total disk space (free + used)
checkSync only takes the path argument. It returns the same info on success, throws an Error on failure.
constdisk=require('diskusage');constos=require('os');letpath=os.platform()==='win32' ? 'c:' : '/';// Callbacksdisk.check(path,function(err,info){if(err){console.log(err);}else{console.log(info.available);console.log(info.free);console.log(info.total);}});// PromiseasyncfunctiongetFreeSpace(path){try{const{ free }=awaitdisk.check(path);console.log(`Free space: ${free}`);returnfree}catch(err){console.error(err)return0}}// Or without using async/awaitdisk.check(path).then(info=>console.log(`free: ${info.free}`)).catch(err=>console.error(err))// Synchronoustry{letinfo=disk.checkSync(path);console.log(info.available);console.log(info.free);console.log(info.total);}catch(err){console.log(err);}The module has an embedded .d.ts file. You can use import * as diskusage from 'diskusage'.
typeDiskUsage={available: number;free: number;total: number;}exportfunctioncheck(path: string,callback: (error?: Error,result?: DiskUsage)=>void): void;exportfunctioncheck(path: string): Promise<DiskUsage>exportfunctioncheckSync(path: string): DiskUsage;To see a demo of this library see the demo/ folder.
You can run it with node: (node 8+ required)
node ./demo/