NodeJs OS Module
The 'os' module in Node.js is a core module that provides several operating system-related utility methods and properties. It allows Node.js applications to interact with the operating system in various ways, such as retrieving system information, managing memory, and interacting with network interfaces. This module is built into Node.js, so you can use it without installing any additional packages.
You can use it by simply requiring the module using the 'require' method.
const os = require('os');
Methods and Properties of 'os' Module
⮞ os.arch()
It returns the CPU architecture of an operating system for which the Node.js binary is compiled. Example: 'x64', 'arm', etc.
const os = require('os');const osArchitecture=os.arch();console.log('CPU Architecture: '+osArchitecture);/*OUTPUTCPU Architecture: x64*/
⮞ os.platform()
It returns the platform of the operating system. Example 'darwin', 'linux', 'win32', etc.
const os = require('os');const platform=os.platform();console.log('OS Platform: '+platform);/*OUTPUTOS Platform: win32*/
⮞ os.hostname()
It returns the hostname of the operating system. Example- DESKTOP-HJU879
const os = require('os');const hostname=os.hostname();console.log('OS Hostname: '+hostname);/*OUTPUTOS Hostname: DESKTOP-J86JKG^7*/
⮞ os.version()
It returns the operating system version as a string. Example - Windows 10 Home Single Language
const os = require('os');const version=os.version();console.log('OS Version: '+version);/*OUTPUTOS Version: Windows 10 Home Single Language*/
⮞ os.type()
Returns the name of the operating system. Example - Windows_NT
const os = require('os');const type=os.type();console.log('OS Name: '+type);/*OUTPUTOS Name: Windows_NT*/
⮞ os.homedir()
Returns the home directory of the current user.
const os = require('os');const homedir=os.homedir();console.log('Home Directory: '+homedir);/*OUTPUTHome Directory: C:\Users\Vaidikalaya*/
⮞ os.tmpdir()
Returns the default directory for temporary files.
const os = require('os');const tmpdir=os.tmpdir();console.log('Temporary Directory: '+tmpdir);/*OUTPUTTemporary Directory: C:\Users\VAIDI~1\AppData\Local\Temp*/
⮞ os.totalmem()
This method returns the total amount of system memory (RAM) available on the machine in bytes.
const os = require('os');const totalMemory=os.totalmem();console.log(`Total RAM: ${(totalMemory / (1024 ** 3)).toFixed(2)} GB`);/*OUTPUTTotal RAM: 11.66 GB*/
⮞ os.freemem()
This method returns the amount of free system memory (RAM) available on the machine in bytes.
const os = require('os');const freeMemory=os.freemem();console.log(`Available RAM: ${(freeMemory / (1024 ** 3)).toFixed(2)} GB`);/*OUTPUTAvailable RAM: 4.84 GB*/
The 'os' module in Node.js lets you interact with the operating system by providing tools to get system information like OS type, platform, and CPU architecture. You can also check free and total memory, access CPU core details, find network interface information, and retrieve user-specific directories like the home or temp directory. This is handy for monitoring resources and adapting your application to the environment it's running in.