Lesson 14 of 50 – OS Module
28% Completed
28%

Node.js OS Module

The Node.js OS module provides information about the operating system on which a Node.js application is running. We can use it to get the operating system name, CPU information, memory usage, home directory, and other system details.

In simple words: The OS module helps us find information about the computer running our Node.js program.

1. What Is the OS Module?

The os module is built into Node.js, so we do not need to install any additional package.

Example: Import the OS Module
const os = require('node:os');

console.log("OS Module Loaded");

Output:

OS Module Loaded

This example uses CommonJS. In an ES Module project, use import os from 'node:os';.

2. os.platform()

The os.platform() method returns the platform identifier for the operating system.

Example: Get the Platform
const os = require('node:os');

console.log(os.platform());

Possible outputs:

win32

Other common platform identifiers include:

  • linux – Linux systems
  • darwin – macOS
  • win32 – Windows

3. os.arch()

The os.arch() method returns the CPU architecture for which the Node.js binary was compiled.

Example: Get the Architecture
const os = require('node:os');

console.log(os.arch());

Possible output:

x64

The output depends on the Node.js build running on your computer. Other possible values include arm64 and ia32.

4. os.hostname()

The os.hostname() method returns the hostname of the operating system.

Example: Get the Computer Name
const os = require('node:os');

console.log(os.hostname());

Possible output:

DESKTOP-ABC123

Your output will depend on your computer's hostname.

5. os.type()

The os.type() method returns the operating system name as reported by Node.js.

Example: Get the OS Type
const os = require('node:os');

console.log(os.type());

Possible outputs:

Windows_NT
Linux
Darwin

The exact output depends on the operating system.

6. os.release()

The os.release() method returns the operating system release version.

Example: Get the OS Release
const os = require('node:os');

console.log(os.release());

Possible output:

10.0.22631

This is an example only. The returned version varies by operating system and installed release.

7. os.totalmem()

The os.totalmem() method returns the total amount of system memory in bytes.

Example: Get Total Memory
const os = require('node:os');

const totalMemory = os.totalmem();

console.log("Total Memory:", totalMemory, "bytes");

console.log(
    "Total Memory:",
    (totalMemory / (1024 ** 3)).toFixed(2),
    "GB"
);

Possible output:

Total Memory: 17179869184 bytes
Total Memory: 16.00 GB

The output above is an example for a system with approximately 16 GiB of memory.

8. os.freemem()

The os.freemem() method returns the amount of free system memory in bytes.

Example: Get Free Memory
const os = require('node:os');

const freeMemory = os.freemem();

console.log(
    "Free Memory:",
    (freeMemory / (1024 ** 3)).toFixed(2),
    "GB"
);

Possible output:

Free Memory: 5.25 GB

Free memory changes as programs start, stop, and use system resources.

9. os.homedir()

The os.homedir() method returns the current user's home directory.

Example: Get the Home Directory
const os = require('node:os');

console.log(os.homedir());

Possible output on Windows:

C:\Users\Student

Possible output on Linux:

/home/student

10. os.tmpdir()

The os.tmpdir() method returns the operating system's default directory for temporary files.

Example: Get the Temporary Directory
const os = require('node:os');

console.log(os.tmpdir());

Possible output on Windows:

C:\Users\Student\AppData\Local\Temp

The path depends on your operating system and environment.

11. os.cpus()

The os.cpus() method returns an array containing information about each logical CPU core, including its model, speed, and time statistics.

Example: Display CPU Information
const os = require('node:os');

const cpus = os.cpus();

console.log("Logical CPU Cores:", cpus.length);
console.log("CPU Model:", cpus[0].model);
console.log("CPU Speed:", cpus[0].speed, "MHz");

Possible output:

Logical CPU Cores: 8
CPU Model: Example Processor
CPU Speed: 2400 MHz

Your CPU count, model, and speed will vary.

12. os.uptime()

The os.uptime() method returns the system uptime in seconds.

Example: Get System Uptime
const os = require('node:os');

const uptime = os.uptime();

console.log("System Uptime:", uptime, "seconds");

console.log(
    "System Uptime:",
    (uptime / 3600).toFixed(2),
    "hours"
);

The returned value depends on how long the system has been running.

13. os.userInfo()

The os.userInfo() method returns information about the current operating system user, such as the username and home directory.

Example: Get User Information
const os = require('node:os');

const user = os.userInfo();

console.log("Username:", user.username);
console.log("Home Directory:", user.homedir);

Possible output:

Username: Student
Home Directory: C:\Users\Student
User information may be sensitive. Avoid exposing it unnecessarily in public application logs or API responses.

14. Common OS Module Methods

Method Purpose
os.platform() Returns the platform identifier.
os.arch() Returns the Node.js CPU architecture.
os.hostname() Returns the system hostname.
os.type() Returns the operating system name.
os.release() Returns the OS release version.
os.totalmem() Returns total system memory in bytes.
os.freemem() Returns free system memory in bytes.
os.homedir() Returns the user's home directory.
os.tmpdir() Returns the temporary directory.
os.cpus() Returns information about logical CPU cores.
os.uptime() Returns system uptime in seconds.
os.userInfo() Returns current OS user information.

15. Practical Assignment

Practice Task
Create a Node.js program that displays information about the computer running the program.
  1. Import the OS module.
  2. Display the operating system platform.
  3. Display the CPU architecture.
  4. Display the hostname.
  5. Display total and free memory in GB.
  6. Display the home directory.
  7. Display the number of logical CPU cores.
  8. Display system uptime in hours.

Starter code:

const os = require('node:os');

console.log("Platform:", os.platform());
console.log("Architecture:", os.arch());
console.log("Hostname:", os.hostname());

console.log(
    "Total Memory:",
    (os.totalmem() / (1024 ** 3)).toFixed(2),
    "GB"
);

console.log(
    "Free Memory:",
    (os.freemem() / (1024 ** 3)).toFixed(2),
    "GB"
);

console.log("Home Directory:", os.homedir());
console.log("CPU Cores:", os.cpus().length);
console.log(
    "Uptime:",
    (os.uptime() / 3600).toFixed(2),
    "hours"
);

🧠 Quiz – Node.js OS Module

Select the correct answer for each question.

1. Which method returns the operating system platform?



2. Which method returns total system memory in bytes?



3. Which method returns information about logical CPU cores?