August, 2019
So we installed NodeJS on our machine.
Now we want to learn how to get information about the operating system by using the OS module.
index.js
:touch index.js
const { platform, arch, release, totalmem, freemem } = require("os");
console.log(`Your Operating System: ${release()} ${platform()} ${arch()}`);
console.log(
`${((freemem() / totalmem()) * 100).toFixed(2)} % of your RAM is free.`
);
Note: I use the most used url properties to decrease the complexity of this simple example. To see all the available properties, read the docs of the OS module. There is a lot of cool stuff.
/*
import the os module & destructure the desired properties/functions
similar to:
const os = require('os');
const { platform, arch, release, totalmem, freemem } = os;
*/
const { platform, arch, release, totalmem, freemem } = require("os");
// log some information about the operating system
console.log(`Your Operating System: ${release()} ${platform()} ${arch()}`);
// log some information about the memory (ram) (number is rounded to two decimals)
console.log(
`${((freemem() / totalmem()) * 100).toFixed(2)} % of your RAM is free.`
);
node index.js
Your Operating System: 5.2.9-arch1-1-ARCH linux x64
18.63 % of your RAM is free.
Hi! I'm Michael 👋 I'm a Mentor & Educator & Senior Web Developer - I help you to reach your (career) goals.