Node.js Tutorial

Node.js OS Module

Node.js OS Module

Node.js OS Module

The Node.js OS module provides useful functions for interacting with the operating system. It allows developers to retrieve system-level information such as CPU details, memory usage, network interfaces, system uptime, directories, hostname, and operating system information.

Using the OS module, you can easily build system-monitoring tools and applications that need information about the environment in which Node.js is running.

Node.js OS Module
Node.js OS Module

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

Frequently Used Node.js OS Methods

MethodDescription
os.arch()Returns the CPU architecture.
os.cpus()Returns information about the available CPU cores, including model and speed.
os.freemem()Returns the amount of free system memory in bytes.
os.totalmem()Returns the total amount of system memory in bytes.
os.homedir()Returns the current user’s home directory.
os.tmpdir()Returns the operating system’s default temporary directory.
os.hostname()Returns the hostname of the operating system.
os.platform()Returns the operating system platform.
os.type()Returns the operating system name.
os.uptime()Returns the system uptime in seconds.
os.networkInterfaces()Returns information about the available network interfaces.

Example 1: Getting Basic OS Information

The following example demonstrates how to retrieve basic operating system information using the Node.js OS module.

// os_example1.js
const os = require('os');

console.log("Free memory:", os.freemem());
console.log("Home dir:", os.homedir());
console.log("Hostname:", os.hostname());
console.log("Platform:", os.platform());
console.log("Total memory:", os.totalmem());
console.log("OS type:", os.type());
console.log("Uptime:", os.uptime());

// Run: node os_example1.js

Explanation

  • os.freemem() returns the available system memory.
  • os.homedir() returns the user’s home directory.
  • os.hostname() returns the system hostname.
  • os.platform() identifies the operating system platform.
  • os.totalmem() returns the total system memory.
  • os.type() returns the operating system name.
  • os.uptime() returns how long the system has been running.

Example 2: Getting CPU and Network Information

The OS module can also be used to retrieve CPU and network-related information from the system.

// os_example2.js
const os = require('os');

console.log("CPUs:", os.cpus());
console.log("Arch:", os.arch());
console.log("Network:", os.networkInterfaces());

// Run: node os_example2.js

Explanation

  • os.cpus() provides details about the system’s CPU cores.
  • os.arch() returns the CPU architecture.
  • os.networkInterfaces() provides information about network interfaces.

Why Use the Node.js OS Module?

The OS module is useful when an application needs to understand the system environment in which it is running. Developers can use it for system monitoring, resource management, performance analysis, and applications that need operating-system-specific information.

YT:- DecodeIT

Final Thought

The Node.js OS module makes it simple to access important system-level information directly from a Node.js application. Methods such as os.cpus(), os.freemem(), os.totalmem(), os.platform(), and os.networkInterfaces() are especially useful for monitoring system resources and building system-aware applications.

Keywords

Node.js OS Module, Node.js OS module tutorial, Node.js os module, Node.js operating system module, os.cpus(), os.freemem(), os.totalmem(), os.arch(), os.platform(), os.hostname(), os.uptime(), os.networkInterfaces(), Node.js system information

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us