0
0
Node.jsframework~10 mins

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

Choose your learning style9 modes available
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.