0
0
NodejsHow-ToBeginner · 4 min read

How to Use os Module in Node.js: Syntax and Examples

In Node.js, you use the os module by importing it with import os from 'os' or const os = require('os'). This module provides methods to get system information like CPU details, memory usage, and network interfaces.
📐

Syntax

The os module is built into Node.js and can be imported using import os from 'os' in ES modules or const os = require('os') in CommonJS. It offers various methods like os.cpus(), os.totalmem(), and os.networkInterfaces() to access system info.

  • import os from 'os': Imports the os module (ESM syntax).
  • const os = require('os'): Imports the os module (CommonJS syntax).
  • os.cpus(): Returns an array of CPU core info.
  • os.totalmem(): Returns total system memory in bytes.
  • os.networkInterfaces(): Returns network interface details.
javascript
import os from 'os';

// Example usage:
const cpus = os.cpus();
const totalMemory = os.totalmem();
const network = os.networkInterfaces();
💻

Example

This example shows how to import the os module and print CPU model, total memory in GB, and network interfaces to the console.

javascript
import os from 'os';

console.log('CPU Model:', os.cpus()[0].model);
console.log('Total Memory (GB):', (os.totalmem() / 1024 / 1024 / 1024).toFixed(2));
console.log('Network Interfaces:', os.networkInterfaces());
Output
CPU Model: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz Total Memory (GB): 16.00 Network Interfaces: { lo0: [ { address: '127.0.0.1', family: 'IPv4', internal: true, ... } ], en0: [ { address: '192.168.1.5', family: 'IPv4', internal: false, ... } ] }
⚠️

Common Pitfalls

Some common mistakes when using the os module include:

  • Forgetting to import or require the module before use.
  • Assuming memory values are in MB or GB instead of bytes.
  • Not handling the array returned by os.cpus() properly.
  • Expecting network interfaces to be simple strings instead of objects with details.

Always check the data types and units returned by os methods.

javascript
/* Wrong: Using os without import */
// console.log(os.platform()); // ReferenceError: os is not defined

/* Right: Import first */
const os = require('os');
console.log(os.platform());
Output
linux
📊

Quick Reference

MethodDescription
os.cpus()Returns an array with info about each CPU/core.
os.totalmem()Returns total system memory in bytes.
os.freemem()Returns free system memory in bytes.
os.platform()Returns the operating system platform (e.g., 'linux', 'win32').
os.networkInterfaces()Returns an object with network interface details.
os.uptime()Returns system uptime in seconds.

Key Takeaways

Import the os module using ES modules or CommonJS syntax before using it.
os methods return system info like CPU, memory, and network details in specific formats and units.
Always convert memory values from bytes to MB or GB for readability.
os.cpus() returns an array; access elements carefully to get CPU details.
Check the structure of networkInterfaces() output as it contains nested objects.