OS Module

OS Module

The OS (Operating System) module provides a suite of methods that let your code interact with the underlying operating system. It provides vital information about the computer's hardware, memory, CPU architecture, and user configurations.

This module is highly useful when you are writing CLI (Command Line Interface) tools or performance monitoring scripts.


1. Getting System Information

To start using it, require the os module:

const os = require('os');

You can instantly fetch details about the system's memory and platform.

Common OS Methods

const os = require('os');

// Get the operating system platform (e.g., 'win32', 'darwin', 'linux') console.log('Platform:', os.platform());

// Get the system architecture (e.g., 'x64', 'arm') console.log('Architecture:', os.arch());

// Get total system memory in bytes console.log('Total Memory:', os.totalmem());

// Get available free memory in bytes console.log('Free Memory:', os.freemem());

// Get information about the current user console.log('User Info:', os.userInfo());


2. Working with CPUs

You can also retrieve a detailed array of objects containing information about every logical CPU core installed on the server using os.cpus(). This is particularly useful when you want to spin up multiple instances of your server utilizing cluster processing.


Exercise

?

Which method returns the amount of free system memory available?