get system free disk space in javascript

To get the free disk space of the system using JavaScript and Node.js, we can use the built-in Node.js module os. Here's an example code snippet that gets the free disk space in bytes:

const os = require('os');

// Get free memory in bytes
const freeDiskSpaceinBytes = os.freemem();

console.log(`Free disk space: ${freeDiskSpaceinBytes} bytes`);
162 chars
7 lines

The os.freemem() method returns the free memory in bytes as an integer. We can use this method to get the free disk space on the system where our code is running.

If you'd like to convert the result to another unit like kilobytes, megabytes, or gigabytes, you can use simple arithmetic operations. Here's an example code snippet that converts the free disk space to gigabytes:

const os = require('os');

// Get free memory in bytes
const freeDiskSpaceinBytes = os.freemem();

// Convert bytes to gigabytes
const freeDiskSpaceinGB = (freeDiskSpaceinBytes / (1024 * 1024 * 1024)).toFixed(2);

console.log(`Free disk space: ${freeDiskSpaceinGB} GB`);
271 chars
10 lines

This will print the free disk space in gigabytes, rounded to two decimal places.

gistlibby LogSnag