OS Module in Node Js

  • The `os` module in Node.js provides a set of operating system-related utility methods. It allows you to access information about the operating system, such as CPU architecture, free memory, platform, and more. Below, I'll explain some of the common methods provided by the `os` module with examples:
Getting OS Information:
  • The `os` module provides the `os.platform()` method to retrieve the operating system platform.


    const os = require('os');
    console.log('Operating System Platform:', os.platform());

Getting CPU Architecture:

  • You can use `os.arch()` to obtain the CPU architecture.


    const os = require('os');
    console.log('CPU Architecture:', os.arch());

Getting CPU Information:

  • The `os.cpus()` method returns an array of objects containing information about each CPU/core.


    const os = require('os');

    const cpus = os.cpus();
    console.log('CPU Information:', cpus);

Getting Total and Free Memory:

  • The `os.totalmem()` method returns the total amount of system memory in bytes, while `os.freemem()` returns the amount of free system memory.


    const os = require('os');

    console.log('Total Memory:', os.totalmem() / (1024 * 1024), 'MB');
    console.log('Free Memory:', os.freemem() / (1024 * 1024), 'MB');

Getting System Uptime:

  • The `os.uptime()` method returns the system uptime in seconds.


    const os = require('os');
    console.log('System Uptime:', os.uptime(), 'seconds');

Getting User Information:

  • The `os.userInfo()` method returns information about the current user.


    const os = require('os');

    const userInfo = os.userInfo();
    console.log('User Information:', userInfo);

Checking Endianness:

  • The `os.endianness()` method returns the endianness of the CPU, which is either `'BE'` (Big Endian) or `'LE'` (Little Endian).


    const os = require('os');
    console.log('Endianness:', os.endianness());

    Getting Hostname:
    • You can use `os.hostname()` to retrieve the host name of the operating system.

        const os = require('os');
        console.log('Hostname:', os.hostname());


    Retrieving Network Interfaces:

    • The `os.networkInterfaces()` method returns an object containing information about the network interfaces on the system.

        const os = require('os');

        const networkInterfaces = os.networkInterfaces();
        console.log('Network Interfaces:', networkInterfaces);


    Getting Load Averages:

    • The `os.loadavg()` method returns an array containing the system load averages for the past 1, 5, and 15 minutes.

        const os = require('os');

        const loadAverages = os.loadavg();
        console.log('Load Averages (1min, 5min, 15min):', loadAverages);


    Retrieving Temporary Directory:

    • The `os.tmpdir()` method returns the operating system's default directory for temporary files.

        const os = require('os');
        console.log('Temporary Directory:', os.tmpdir());


    Getting Environment Information:

    • The `os.EOL` property provides the end-of-line marker for the operating system. It can be used for consistent newline handling.

        const os = require('os');
        console.log('End of Line Marker:', os.EOL);


    Checking Platform Specifics:

    • The `os.constants` object provides various constants related to operating system signals, error codes, and more.

        const os = require('os');
        console.log('Operating System Constants:', os.constants);


    Checking CPU Usage:

    • The `os.cpus()` method, mentioned earlier, also provides information about CPU usage.

        const os = require('os');

        const cpus = os.cpus();
        cpus.forEach((cpu, index) => {
            console.log(`CPU ${index + 1}:`);
            console.log('  Model:', cpu.model);
            console.log('  Speed:', cpu.speed, 'MHz');
            console.log('  Times:', cpu.times);
        });

    • These examples demonstrate some of the functionalities provided by the `os` module in Node.js. The module offers additional methods and information, so you can explore its documentation for a comprehensive list of available features. Keep in mind that certain methods might not be supported on all operating systems, so it's essential to check the documentation for cross-platform compatibility. Explore the official documentation (https://nodejs.org/api/os.html) for the `os` module to discover further functionalities and details about each method.

    No comments:

    Post a Comment