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.
The os module is built into Node.js, so we do not
need to install any additional package.
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';.
The os.platform() method returns the platform
identifier for the operating system.
const os = require('node:os');
console.log(os.platform());
Possible outputs:
win32
Other common platform identifiers include:
linux – Linux systemsdarwin – macOSwin32 – Windows
The os.arch() method returns the CPU architecture
for which the Node.js binary was compiled.
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.
The os.hostname() method returns the hostname
of the operating system.
const os = require('node:os');
console.log(os.hostname());
Possible output:
DESKTOP-ABC123
Your output will depend on your computer's hostname.
The os.type() method returns the operating
system name as reported by Node.js.
const os = require('node:os');
console.log(os.type());
Possible outputs:
Windows_NT
Linux
Darwin
The exact output depends on the operating system.
The os.release() method returns the operating
system release version.
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.
The os.totalmem() method returns the total amount
of system memory in bytes.
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.
The os.freemem() method returns the amount of
free system memory in bytes.
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.
The os.homedir() method returns the current
user's home directory.
const os = require('node:os');
console.log(os.homedir());
Possible output on Windows:
C:\Users\Student
Possible output on Linux:
/home/student
The os.tmpdir() method returns the operating
system's default directory for temporary files.
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.
The os.cpus() method returns an array containing
information about each logical CPU core, including its model,
speed, and time statistics.
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.
The os.uptime() method returns the system uptime
in seconds.
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.
The os.userInfo() method returns information
about the current operating system user, such as the username
and home directory.
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
| 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. |
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"
);
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?