Skip to Content
Node.js9. OS Module 🧠

os module - The System’s Information Hub

The os module is a built-in Node.js utility that provides methods and properties for retrieving information about the operating system, the current user, and the hardware on which your application is running.

It’s a crucial module for building applications that need to adapt their behavior based on the host environment or for gathering system metrics for logging and monitoring. Since it’s a core module, you simply need to import it: const os = require('os');.

👤 System and User Information

These methods help you identify the environment your code is executing in.

  • os.platform(): Returns a string identifying the platform (e.g., 'darwin' for macOS, 'win32' for Windows, 'linux' for Linux). This is the best method for writing platform-specific code.
  • os.type(): Returns the operating system name (e.g., 'Darwin', 'Windows_NT', 'Linux').
  • os.release(): Returns the specific release version of the operating system.
  • os.homedir(): Returns the path to the current user’s home directory. This is extremely useful for locating configuration files.
  • os.hostname(): Returns the hostname of the machine.
  • os.userInfo(): Returns an object containing details about the current user, including their username.
  • os.uptime(): Returns the number of seconds the system has been running.

Example:

userInfo.js
const os = require("os"); console.log("Platform:", os.platform()); console.log("System Type:", os.type()); console.log("Release:", os.release()); console.log("Home Directory:", os.homedir()); console.log("Hostname:", os.hostname()); console.log("Current User:", os.userInfo().username); console.log("System Uptime:", Math.floor(os.uptime() / 3600) + " hours");

⚙️ Hardware and Performance Metrics

These methods give you insight into the system’s hardware and its current performance load.

  • os.arch(): Returns the CPU architecture (e.g., 'x64' or 'arm64').
  • os.cpus(): Returns an array of objects, where each object provides detailed information about a logical CPU core (model, speed, and usage times).
  • os.totalmem(): Returns the total amount of system memory in bytes.
  • os.freemem(): Returns the amount of free system memory in bytes.

Example:

hwInfo.js
const os = require("os"); // Get CPU core information console.log("CPU Architecture:", os.arch()); console.log("Number of CPU cores:", os.cpus().length); console.log("First CPU Core Model:", os.cpus()[0].model); // Get memory information const totalMemoryGB = (os.totalmem() / 1024 / 1024 / 1024).toFixed(2); const freeMemoryGB = (os.freemem() / 1024 / 1024 / 1024).toFixed(2); console.log(`Memory: ${freeMemoryGB}GB free / ${totalMemoryGB}GB total`);

🌐 Networking Information

The os module can also inspect the network interfaces available on the machine.

  • os.networkInterfaces(): Returns an object where each key is the name of a network interface (e.g., 'en0' or 'Ethernet'). The value is an array of objects, each describing an assigned network address (including IPv4, IPv6, and MAC address).

Example:

networkInfo.js
const os = require("os"); // Get network interfaces console.log("Network Interfaces:", os.networkInterfaces());

📋 Useful Constants

  • os.EOL: This property provides the operating system-specific End-Of-Line marker ('\r\n' on Windows and '\n' on POSIX). This is essential for writing text files that are compatible across different platforms.
  • os.constants: An object containing common OS constants for things like error codes and process signals (e.g., os.constants.errno.EADDRINUSE).
Last updated on