Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using the os Module for System Information in Node.js
📖 Scenario: You want to create a small Node.js script that shows some basic information about your computer system. This helps you understand how to use the os module to get details like your computer's platform, CPU cores, and memory.
🎯 Goal: Build a Node.js script that uses the os module to display the system platform, number of CPU cores, and total memory.
📋 What You'll Learn
Import the os module
Create a variable to hold the system platform
Create a variable to hold the number of CPU cores
Create a variable to hold the total system memory
Use the os module methods to get the above information
💡 Why This Matters
🌍 Real World
Developers often need to get system information to optimize apps or show system stats in tools.
💼 Career
Knowing how to use built-in Node.js modules like os is essential for backend development and system scripting.
Progress0 / 4 steps
1
Import the os module
Write a line to import the os module using require and assign it to a variable called os.
Node.js
Hint
Use const os = require('os'); to import the module.
2
Get the system platform
Create a variable called platform and set it to the result of calling os.platform().
Node.js
Hint
Use os.platform() to get the platform string.
3
Get the number of CPU cores
Create a variable called cpuCount and set it to the length of the array returned by os.cpus().
Node.js
Hint
Use os.cpus() to get an array of CPUs, then get its length.
4
Get the total system memory
Create a variable called totalMemory and set it to the result of calling os.totalmem().
Node.js
Hint
Use os.totalmem() to get the total memory in bytes.
Practice
(1/5)
1. What does the Node.js os module primarily provide?
easy
A. Information about the operating system and hardware
B. Functions to create web servers
C. Tools for database management
D. Methods to handle file uploads
Solution
Step 1: Understand the purpose of the os module
The os module 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 the os module.
Final Answer:
Information about the operating system and hardware -> Option A
Quick 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
A. require os = 'os';
B. import os from 'os';
C. const os = import('os');
D. const os = require('os');
Solution
Step 1: Recall Node.js module import syntax
In Node.js, the common way to import built-in modules is using const 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 D
Quick 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
A. The hostname of the system
B. The number of CPU cores on the system
C. The total amount of system memory in bytes
D. An error because cpus is not a function
Solution
Step 1: Understand os.cpus() method
The os.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.
const os = require('os');
console.log(os.totalmem());
medium
A. No error; it correctly logs total memory
B. totalmem is not a function, should be totalMem
C. Missing parentheses after totalmem
D. Should import os with import statement
Solution
Step 1: Check method name correctness
The correct method to get total memory is os.totalmem() all lowercase, so spelling is correct.
Step 2: Verify method usage
The code uses os.totalmem() correctly with parentheses, so no syntax error.
Step 3: Re-examine options
totalmem is not a function, should be totalMem claims totalMem is correct, but Node.js uses totalmem lowercase. So totalmem is not a function, should be totalMem is incorrect.
Step 4: Identify actual error
There is no error; the code is correct.
Final Answer:
No error; it correctly logs total memory -> Option A
Quick 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
A. const os = require('os');
console.log(`Free memory: ${os.freemem / os.totalmem * 100}%`);
B. const os = require('os');
const free = os.totalmem();
const total = os.freemem();
console.log(`Free memory: ${(free / total * 100).toFixed(2)}%`);
C. const os = require('os');
const free = os.freemem();
const total = os.totalmem();
console.log(`Free memory: ${(free / total * 100).toFixed(2)}%`);
D. const os = require('os');
const free = os.freemem();
const total = os.totalmem();
console.log('Free memory: ' + free + '/' + total + '%');
Solution
Step 1: Identify correct methods for free and total memory
os.freemem() returns free memory, and os.totalmem() returns total memory.
Step 2: Calculate percentage and format output
Divide free by total, multiply by 100, and use toFixed(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 C
Quick Check:
free/total * 100 with toFixed(2) = correct percentage [OK]
Hint: Divide freemem() by totalmem(), multiply by 100, format decimals [OK]