Node.js OS Module Practice Questions with Solution

Introduction

The Node.js os module provides useful information about the computer and operating system running your Node.js application. You can use it to find the operating system platform, CPU architecture, CPU information, memory, home directory, hostname, and more. In this chapter, you will practice important os module methods with simple, step-by-step examples that are useful for beginners and real-world Node.js applications. Node.js OS Module practice questions with solution help to understand the concepts.

Question 1: How do you import the OS module in Node.js?

Problem

Import the built-in os module and display a message confirming that it is working.

Solution

const os = require("os");

console.log("OS module loaded successfully.");

Output

OS module loaded successfully.

Step-by-Step Explanation

  1. Import the built-in os module using require().
  2. Store it in the os variable.
  3. Use console.log() to display a message.
  4. The os module does not need to be installed separately.

Question 2: How do you find the operating system platform?

Problem

Use the os module to find the platform on which Node.js is running.

Solution

const os = require("os");

console.log("Platform:", os.platform());

Output

The output depends on your operating system.

For example, on Windows:

Platform: win32

On Linux:

Platform: linux

Step-by-Step Explanation

  1. Import the os module.
  2. Call os.platform().
  3. Node.js returns the operating system platform.
  4. Display the result.

Common values include:

win32
linux
darwin

Question 3: How do you find the CPU architecture?

Problem

Find the CPU architecture of the computer running your Node.js program.

Solution

const os = require("os");

console.log("CPU Architecture:", os.arch());

Output

A common output is:

CPU Architecture: x64

Step-by-Step Explanation

  1. Import the os module.
  2. Call os.arch().
  3. Node.js returns the CPU architecture.
  4. Display the result.

Depending on the system, you may see values such as x64, arm64, or another supported architecture.


Question 4: How do you find the computer hostname?

Problem

Display the hostname of the computer running your Node.js application.

Solution

const os = require("os");

const hostname = os.hostname();

console.log("Hostname:", hostname);

Output

The exact output depends on your computer.

Hostname: DESKTOP-ABC123

Step-by-Step Explanation

  1. Import the os module.
  2. Use os.hostname().
  3. Store the returned hostname in hostname.
  4. Display it.

The hostname identifies the computer on a network.


Question 5: How do you find the user’s home directory?

Problem

Display the home directory of the current user.

Solution

const os = require("os");

const homeDirectory = os.homedir();

console.log("Home Directory:", homeDirectory);

Output

The exact output depends on your computer.

For example:

Home Directory: C:\Users\Riya

Step-by-Step Explanation

  1. Import the os module.
  2. Call os.homedir().
  3. Node.js returns the current user’s home directory.
  4. Display the result.

This can be useful when an application needs to work with files stored in the user’s home directory.


Question 6: How do you find the temporary directory?

Problem

Use the os module to find the operating system’s temporary directory.

Solution

const os = require("os");

console.log("Temporary Directory:", os.tmpdir());

Output

The exact output depends on the operating system.

For example:

Temporary Directory: C:\Users\Riya\AppData\Local\Temp

Step-by-Step Explanation

  1. Import os.
  2. Call os.tmpdir().
  3. Node.js returns the default temporary directory.
  4. Display the path.

Temporary directories are commonly used for temporary files and data.


Question 7: How do you find the number of CPU cores?

Problem

Find the number of CPU entries available to Node.js.

Solution

const os = require("os");

const cpus = os.cpus();

console.log("CPU Count:", cpus.length);

Output

The result depends on the computer.

CPU Count: 8

Step-by-Step Explanation

  1. Import the os module.
  2. Call os.cpus().
  3. It returns information about the available logical CPUs.
  4. Use .length to count the returned entries.
  5. Display the count.

Important Point

os.cpus().length represents the number of logical CPU entries reported by Node.js. It should not automatically be interpreted as the number of physical CPU cores.


Question 8: How do you find total and free system memory?

Problem

Display the total and currently free system memory in bytes.

Solution

const os = require("os");

const totalMemory = os.totalmem();
const freeMemory = os.freemem();

console.log("Total Memory:", totalMemory);
console.log("Free Memory:", freeMemory);

Output

The exact values depend on your computer.

Total Memory: 17179869184
Free Memory: 6248458240

Step-by-Step Explanation

  1. Import the os module.
  2. os.totalmem() returns total system memory in bytes.
  3. os.freemem() returns the amount of free system memory in bytes.
  4. Store both values.
  5. Display them.

Large byte values can be difficult to understand, so we can convert them into GB.

For example:

const os = require("os");

const totalGB = os.totalmem() / (1024 ** 3);
const freeGB = os.freemem() / (1024 ** 3);

console.log(
    "Total Memory:",
    totalGB.toFixed(2),
    "GB"
);

console.log(
    "Free Memory:",
    freeGB.toFixed(2),
    "GB"
);

Output

Example:

Total Memory: 16.00 GB
Free Memory: 5.82 GB

Question 9: How do you display detailed CPU information?

Problem

Use the os module to display information about the first CPU entry.

Solution

const os = require("os");

const cpus = os.cpus();

console.log(cpus[0]);

Output

The exact output depends on the computer.

A simplified example may look like:

{
  model: 'Intel(R) Core(TM) i5',
  speed: 2400,
  times: {
    user: 123456,
    nice: 0,
    sys: 45678,
    idle: 789012,
    irq: 0
  }
}

Step-by-Step Explanation

  1. Import the os module.
  2. Call os.cpus().
  3. Store the returned array in cpus.
  4. Use cpus[0] to access the first CPU entry.
  5. Display the information.

Each CPU entry can contain information such as:

  • CPU model
  • CPU speed
  • User time
  • System time
  • Idle time
  • Interrupt time

Question 10: How do you create a system information dashboard using the OS module?

Problem

Create a simple Node.js program that displays:

  • Platform
  • Architecture
  • Hostname
  • Home directory
  • CPU count
  • Total memory
  • Free memory
  • Temporary directory

Solution

const os = require("os");

const totalMemory =
    os.totalmem() / (1024 ** 3);

const freeMemory =
    os.freemem() / (1024 ** 3);

console.log("===== SYSTEM INFORMATION =====");

console.log("Platform:", os.platform());

console.log("Architecture:", os.arch());

console.log("Hostname:", os.hostname());

console.log("Home Directory:", os.homedir());

console.log("CPU Count:", os.cpus().length);

console.log(
    "Total Memory:",
    totalMemory.toFixed(2),
    "GB"
);

console.log(
    "Free Memory:",
    freeMemory.toFixed(2),
    "GB"
);

console.log("Temporary Directory:", os.tmpdir());

Output

The exact values depend on your computer.

Example:

===== SYSTEM INFORMATION =====
Platform: win32
Architecture: x64
Hostname: DESKTOP-ABC123
Home Directory: C:\Users\Riya
CPU Count: 8
Total Memory: 16.00 GB
Free Memory: 5.82 GB
Temporary Directory: C:\Users\Riya\AppData\Local\Temp

Step-by-Step Explanation

  1. Import the os module.
  2. Get total system memory using os.totalmem().
  3. Get free memory using os.freemem().
  4. Convert bytes into GB.
  5. Get the platform using os.platform().
  6. Get the architecture using os.arch().
  7. Get the hostname using os.hostname().
  8. Get the home directory using os.homedir().
  9. Get CPU information using os.cpus().
  10. Get the temporary directory using os.tmpdir().
  11. Display all the information.

This is a simple example of how Node.js can inspect the environment where an application is running.

Key Takeaways

  • The os module is a built-in Node.js module.
  • You can import it using require("os").
  • os.platform() returns the operating system platform.
  • os.arch() returns the CPU architecture.
  • os.hostname() returns the computer hostname.
  • os.homedir() returns the current user’s home directory.
  • os.tmpdir() returns the operating system’s temporary directory.
  • os.cpus() returns CPU information.
  • os.cpus().length can be used to count logical CPU entries.
  • os.totalmem() returns total system memory in bytes.
  • os.freemem() returns currently free system memory in bytes.
  • Memory values returned by these methods are measured in bytes.
  • The exact output of many os methods depends on the computer running the Node.js program.
  • The OS module is useful for system-information tools, diagnostics, and applications that need to understand their runtime environment.

FAQs

1. What is the OS module in Node.js?

The os module is a built-in Node.js module that provides information about the operating system and computer environment where a Node.js application is running.

2. Do I need to install the OS module?

No. The os module comes with Node.js.

You can directly use:

const os = require("os");

3. How do I find the operating system in Node.js?

Use os.platform():

const os = require("os");

console.log(os.platform());

For example, Windows commonly returns:

win32

4. How do I check the CPU architecture in Node.js?

Use os.arch():

const os = require("os");

console.log(os.arch());

A common result on a 64-bit system is:

x64

5. How do I check RAM information using Node.js?

You can use os.totalmem() and os.freemem():

const os = require("os");

console.log("Total:", os.totalmem());
console.log("Free:", os.freemem());

Both values are returned in bytes.

6. How do I find the computer hostname in Node.js?

Use os.hostname():

const os = require("os");

console.log(os.hostname());

The returned value depends on the computer’s hostname.

7. What is the difference between os.homedir() and os.tmpdir()?

os.homedir() returns the current user’s home directory.

os.homedir();

os.tmpdir() returns the operating system’s temporary directory.

os.tmpdir();

They usually point to different locations and serve different purposes.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top