Bird
Raised Fist0
Node.jsframework~10 mins

os module for system information in Node.js - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

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
Concept Flow - os module for system information
Import os module
Call os.method()
Retrieve system info
Return info to program
Display or use info
The program imports the os module, calls a method to get system info, then receives and uses that info.
Execution Sample
Node.js
import os from 'node:os';

const platform = os.platform();
console.log(platform);

const cpus = os.cpus();
const cpuCount = cpus.length;
console.log(cpuCount);
This code prints the current operating system platform and the number of CPU cores.
Execution Table
StepActionMethod CalledReturned ValueOutput
1Import os moduleN/Aos module object loadedNo output
2Call os.platform()os.platform()e.g. 'linux''linux' printed
3Call os.cpus()os.cpus()Array of CPU info objectsNo output
4Access length of CPUs arrayN/Ae.g. 88 printed
5End of scriptN/AN/AScript ends
💡 Script ends after printing platform and CPU count
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
osundefinedos module objectos module objectos module objectos module object
platformundefined'linux''linux''linux''linux'
cpusundefinedundefinedArray of CPU info objectsArray of CPU info objectsArray of CPU info objects
cpuCountundefinedundefinedundefined88
Key Moments - 3 Insights
Why do we call os.platform() as a function with parentheses?
Because os.platform is a function that returns the platform string. The parentheses run the function and get the value, as shown in step 2 of the execution_table.
What does os.cpus() return and why do we use .length on it?
os.cpus() returns an array with info about each CPU core. Using .length gets the number of CPU cores, as shown in steps 3 and 4.
Why is there no output when importing the os module?
Importing loads the module into the program but does not print anything. Output happens only when we call console.log, as in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 2?
ANumber of CPU cores
B'linux' or the current OS platform
CArray of CPU info objects
DNo output
💡 Hint
Check the 'Output' column at step 2 in the execution_table
At which step does the program find out how many CPU cores the system has?
AStep 2
BStep 3
CStep 4
DStep 1
💡 Hint
Look at the 'Action' and 'Output' columns in steps 3 and 4
If we remove parentheses from os.platform(), what happens?
AIt prints the function code instead of the platform
BIt prints the platform string anyway
CIt causes a syntax error
DIt prints undefined
💡 Hint
Recall that os.platform is a function; without (), it refers to the function itself, not its result
Concept Snapshot
os module provides system info functions
Import with: import os from 'node:os';
Use os.platform() to get OS name
Use os.cpus() to get CPU info array
Access .length for CPU count
Call functions with () to get values
Full Transcript
This lesson shows how Node.js's os module gives system information. First, the os module is imported. Then, calling os.platform() returns the operating system name like 'linux'. Calling os.cpus() returns an array describing each CPU core. We get the number of CPU cores by checking the length of this array. The code prints these values using console.log. Importing the module alone does not print anything. Functions must be called with parentheses to get their results. This step-by-step trace helps beginners see how system info is retrieved and used.

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

  1. 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.
  2. 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.
  3. Final Answer:

    Information about the operating system and hardware -> Option A
  4. 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

  1. 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');.
  2. 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.
  3. Final Answer:

    const os = require('os'); -> Option D
  4. 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

  1. Step 1: Understand os.cpus() method

    The os.cpus() method returns an array of objects, each representing a CPU core.
  2. Step 2: Analyze the code output

    The code logs the length of this array, which equals the number of CPU cores on the system.
  3. Final Answer:

    The number of CPU cores on the system -> Option B
  4. Quick 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
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

  1. Step 1: Check method name correctness

    The correct method to get total memory is os.totalmem() all lowercase, so spelling is correct.
  2. Step 2: Verify method usage

    The code uses os.totalmem() correctly with parentheses, so no syntax error.
  3. 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.
  4. Step 4: Identify actual error

    There is no error; the code is correct.
  5. Final Answer:

    No error; it correctly logs total memory -> Option A
  6. 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

  1. Step 1: Identify correct methods for free and total memory

    os.freemem() returns free memory, and os.totalmem() returns total memory.
  2. 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.
  3. 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.
  4. 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
  5. Quick 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