Open In App

What is the os module in NodeJS used for?

The OS module in NodeJS provides various utilities related to the operating system. It allows you to retrieve information about the current operating system, network interfaces, system uptime, and more.

What is the OS Module?

The OS module is a built-in module in NodeJS that provides operating system-related functionality. It allows you to access information about the underlying operating system on which the NodeJS application is running.



Why Use the OS Module?

The OS module is useful for tasks that require information about the operating system environment. This can include tasks such as determining the platform on which the application is running, retrieving information about system resources, or working with network interfaces.

How to Use the OS Module:

The OS module provides various functions and properties to access information about the operating system. Some of the commonly used functions and properties include:



Example: In this example, various functions and properties of the os module are used to retrieve information about the operating system, including the platform, architecture, hostname, memory usage, CPU details, network interfaces, and system uptime.

const os = require('os');

console.log('Platform:', os.platform());
console.log('Architecture:', os.arch());
console.log('Hostname:', os.hostname());
console.log('Total Memory:', os.totalmem() / (1024 * 1024), 'MB');
console.log('Free Memory:', os.freemem() / (1024 * 1024), 'MB');
console.log('CPUs:', os.cpus().length);
console.log('Network Interfaces:', os.networkInterfaces());
console.log('Uptime:', os.uptime(), 'seconds');
Article Tags :