The os module helps you get details about the computer your program is running on. It tells you things like the computer's name, memory, and CPU info.
os module for system information in Node.js
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Node.js
import os from 'os'; // Get system info const hostname = os.hostname(); const platform = os.platform(); const freeMem = os.freemem(); const totalMem = os.totalmem(); const cpus = os.cpus();
Use import os from 'os' in Node.js 20+ with ES modules.
Each function returns info about the system, like hostname() returns the computer's name.
Examples
Node.js
import os from 'os'; console.log(os.hostname());
Node.js
import os from 'os'; console.log(os.platform());
Node.js
import os from 'os'; console.log(os.freemem());
Node.js
import os from 'os'; console.log(os.cpus().length);
Sample Program
This program prints basic system info: the computer's name, OS platform, total and free memory in megabytes, and the number of CPU cores.
Node.js
import os from 'os'; console.log('System Information:'); console.log(`Hostname: ${os.hostname()}`); console.log(`Platform: ${os.platform()}`); console.log(`Total Memory: ${(os.totalmem() / 1024 / 1024).toFixed(2)} MB`); console.log(`Free Memory: ${(os.freemem() / 1024 / 1024).toFixed(2)} MB`); console.log(`CPU Cores: ${os.cpus().length}`);
Important Notes
Memory values are in bytes, so convert to MB or GB for easier reading.
CPU info includes details like model and speed, but here we just count cores.
Output will vary depending on the actual computer running the code.
Summary
The os module gives you easy access to system details.
Use it to get info like hostname, platform, memory, and CPU cores.
This helps your program adapt or report useful system info.
Practice
1. What does the Node.js
os module primarily provide?easy
Solution
Step 1: Understand the purpose of the
Theosmoduleosmodule in Node.js is designed to provide details about the operating system and hardware, such as CPU info, memory, and platform.Step 2: Compare with other options
Options A, C, and D relate to web servers, databases, and file handling, which are not the focus of theosmodule.Final Answer:
Information about the operating system and hardware -> Option AQuick Check:
os module = system info [OK]
Hint: Remember: os module = operating system info [OK]
Common Mistakes:
- Confusing os module with http or fs modules
- Thinking os manages databases or servers
2. Which of the following is the correct way to import the
os module in Node.js?easy
Solution
Step 1: Recall Node.js module import syntax
In Node.js, the common way to import built-in modules is usingconst module = require('module-name');.Step 2: Check each option
const os = require('os'); uses correct syntax. import os from 'os'; is ES module syntax but requires special setup. const os = import('os'); is invalid syntax. require os = 'os'; is incorrect assignment.Final Answer:
const os = require('os'); -> Option DQuick Check:
require('os') = correct import [OK]
Hint: Use require('os') to import os module in Node.js [OK]
Common Mistakes:
- Using ES module import without config
- Wrong assignment syntax
- Confusing import() with require()
3. What will the following code output if run on a typical system?
const os = require('os');
console.log(os.cpus().length);medium
Solution
Step 1: Understand
Theos.cpus()methodos.cpus()method returns an array of objects, each representing a CPU core.Step 2: Analyze the code output
The code logs the length of this array, which equals the number of CPU cores on the system.Final Answer:
The number of CPU cores on the system -> Option BQuick Check:
os.cpus().length = CPU cores count [OK]
Hint: os.cpus() returns array of cores; length = core count [OK]
Common Mistakes:
- Thinking it returns memory size
- Assuming it returns hostname
- Believing cpus() is not a function
4. Identify the error in this code snippet:
const os = require('os');
console.log(os.totalmem());medium
Solution
Step 1: Check method name correctness
The correct method to get total memory isos.totalmem()all lowercase, so spelling is correct.Step 2: Verify method usage
The code usesos.totalmem()correctly with parentheses, so no syntax error.Step 3: Re-examine options
totalmemis not a function, should betotalMemclaimstotalMemis correct, but Node.js usestotalmemlowercase. Sototalmemis not a function, should betotalMemis incorrect.Step 4: Identify actual error
There is no error; the code is correct.Final Answer:
No error; it correctly logs total memory -> Option AQuick Check:
os.totalmem() = total memory [OK]
Hint: Check exact method names in docs; totalmem is lowercase [OK]
Common Mistakes:
- Capitalizing method names incorrectly
- Forgetting parentheses on function calls
- Confusing import styles
5. You want to write a Node.js script that prints the system's free memory as a percentage of total memory using the
os module. Which code snippet correctly does this?hard
Solution
Step 1: Identify correct methods for free and total memory
os.freemem()returns free memory, andos.totalmem()returns total memory.Step 2: Calculate percentage and format output
Divide free by total, multiply by 100, and usetoFixed(2)to show two decimals. const os = require('os'); const free = os.freemem(); const total = os.totalmem(); console.log(`Free memory: ${(free / total * 100).toFixed(2)}%`); does this correctly.Step 3: Check other options for errors
const os = require('os'); const free = os.totalmem(); const total = os.freemem(); console.log(`Free memory: ${(free / total * 100).toFixed(2)}%`); swaps free and total memory, giving wrong result. const os = require('os'); console.log(`Free memory: ${os.freemem / os.totalmem * 100}%`); misses parentheses on functions. const os = require('os'); const free = os.freemem(); const total = os.totalmem(); console.log('Free memory: ' + free + '/' + total + '%'); prints raw numbers without percentage calculation.Final Answer:
const os = require('os'); const free = os.freemem(); const total = os.totalmem(); console.log(`Free memory: ${(free / total * 100).toFixed(2)}%`); -> Option CQuick Check:
free/total * 100 with toFixed(2) = correct percentage [OK]
Hint: Divide freemem() by totalmem(), multiply by 100, format decimals [OK]
Common Mistakes:
- Swapping free and total memory values
- Forgetting parentheses on freemem() or totalmem()
- Not formatting output as percentage
