The os (operating system) module in Node.js provides a set of utility methods to interact with the underlying operating system.
It allows us to access information about the system, such as the platform, network interfaces, memory, and more.
System Information: we can retrieve information about the machines, such as CPU architecture, memory usage by applications, and uptime.
Platform Detection: we can retrieve the information about the operating system platform and its version.
Resource Management: Monitor system resources such as memory and CPU usage for performance optimization.
Network Configuration: Collect information about network interfaces for networking tasks or system diagnostics.
Commonly used methods in the os module:
Returns a string identifying the operating system platform.
const os = require('os'); console.log(os.platform());
Output can be: 'darwin', 'win64', 'win32', 'linux', etc.
Returns a string identifying the operating system CPU architecture.
const os = require('os'); console.log(os.arch());
Output can be: 'x64', 'arm', 'ia32', etc.
Returns an array of objects containing information about each CPU/core.
const os = require('os'); console.log(os.cpus());
Output: will return array of length of cpus core. [ { model: 'M2', speed: 3200, times: { ... } }, ... ]
Returns the total amount of system memory and the amount of free system memory, in bytes.
const os = require('os'); console.log(os.totalmem()); console.log(os.freemem());
Returns the hostname of the operating system.
const os = require('os'); console.log(os.hostname());
Mac.local // Output: will be different on your machine
Returns the operating system release.
const os = require('os'); console.log(os.release());
22.2.2 // Output: can be different on your machine
These are just a few examples of the functionalities provided by the os module in Node.js.
The os module is useful for obtaining information about the system while the Node.js application is running, allowing us to make informed decisions based on the underlying hardware and operating system.