statfs Reports Filesystem Free Space

Read volume-level numbers with fs.statfs: block size, total and free space, unprivileged availability, inode slots, and a used-percent calculation.

JavascriptCli Tooling

Code

javascriptsnippet
15 lines
// fs.statfs reads volume-level numbers for the filesystem holding a path
import { statfs } from 'node:fs/promises';

const stats = await statfs(process.cwd());
const blockBytes = stats.bsize; // one block in bytes
const toGiB = (blocks) => ((blocks * blockBytes) / 1024 ** 3).toFixed(2);

console.log('volume for path:', process.cwd());
console.log('block size:', blockBytes, 'bytes');
// bfree counts all free blocks; bavail only what unprivileged users may use
console.log('total space:', toGiB(stats.blocks), 'GiB');
console.log('free space:', toGiB(stats.bfree), 'GiB');
console.log('available to unprivileged users:', toGiB(stats.bavail), 'GiB');
console.log('used percent:', ((1 - stats.bfree / stats.blocks) * 100).toFixed(1) + '%');
console.log('inode slots total/free:', stats.files, '/', stats.ffree);

Output

Captured from a sandboxed run of this snippet in the build pipeline.

volume for path: C:\Users\GUANGM~1\AppData\Local\Temp\devex-run-scq4FA
block size: 4096 bytes
total space: 236.99 GiB
free space: 11.43 GiB
available to unprivileged users: 11.43 GiB
used percent: 95.2%
inode slots total/free: 0 / 0

How it works

The code’s whole job belongs to the function toGiB.

Asynchronous boundaries use await, so failures surface as rejected promises at the call site instead of being swallowed. The only import source is node:fs/promises, a Node.js built-in, exposing statfs.

What to watch for

  • node:fs/promises is a Node.js built-in module — it ships with the runtime, so there is nothing to install.

Using it

Entry signature: const toGiB = (blocks)

The run emits 7 labeled sections — volume for path:, block size:, total space:, free space:, available to unprivileged users:, used percent:, inode slots total/free: — one per step.

Key statements

const toGiB = (blocks) => ((blocks * blockBytes) / 1024 ** 3).toFixed(2);
const stats = await statfs(process.cwd());

Interactive Tools for This Topic

FAQ

Does this require external packages?
No — `node:fs/promises` is a Node.js built-in module; nothing to install.