Node.js OS Module
Node.js OS Module
The Node.js OS module provides useful utility functions that interact with the operating system. These methods help developers access information such as CPU details, memory usage, network interfaces, and system uptime, making it easier to build system-aware applications.
In this article, we’ll explore some of the most commonly used OS methods in Node.js.
Introduction to Applied AI:–Click Here
Frequently Used Node.js OS Methods
Method | Description |
---|---|
os.arch() | Returns the operating system CPU architecture. |
os.cpus() | Provides an array of objects with details about each CPU/core, such as model, speed (in MHz), and time spent in different states. |
os.endianness() | Returns the CPU endianness: 'BE' (Big Endian) or 'LE' (Little Endian). |
os.freemem() | Returns the amount of free system memory in bytes. |
os.homedir() | Returns the current user’s home directory. |
os.hostname() | Returns the hostname of the operating system. |
os.loadavg() | Returns an array with the load averages for the last 1, 5, and 15 minutes. |
os.networkInterfaces() | Returns details about the available network interfaces. |
os.platform() | Returns the OS platform such as 'darwin' , 'win32' , 'linux' , etc. |
os.release() | Returns the operating system release. |
os.tmpdir() | Returns the default directory for temporary files. |
os.totalmem() | Returns the total amount of system memory in bytes. |
os.type() | Returns the OS name ('Linux' , 'Darwin' , 'Windows_NT' , etc.). |
os.uptime() | Returns the system uptime in seconds. |
os.userInfo([options]) | Returns user account details of the current effective user. |
Data Science Tutorial:-Click Here
Node.js OS Example 1
In the first example, let’s try out some of the basic OS functions.
File: os_example1.js
const os = require('os');
console.log("os.freemem(): \n", os.freemem());
console.log("os.homedir(): \n", os.homedir());
console.log("os.hostname(): \n", os.hostname());
console.log("os.endianness(): \n", os.endianness());
console.log("os.loadavg(): \n", os.loadavg());
console.log("os.platform(): \n", os.platform());
console.log("os.release(): \n", os.release());
console.log("os.tmpdir(): \n", os.tmpdir());
console.log("os.totalmem(): \n", os.totalmem());
console.log("os.type(): \n", os.type());
console.log("os.uptime(): \n", os.uptime());
Run the file using:
node os_example1.js
Download New Real Time Projects :-Click here
Node.js OS Example 2
In the second example, we’ll look at the remaining methods.
File: os_example2.js
const os = require('os');
console.log("os.cpus(): \n", os.cpus());
console.log("os.arch(): \n", os.arch());
console.log("os.networkInterfaces(): \n", os.networkInterfaces());
Run the file using:
node os_example2.js
Machine Learning Tutorial:–Click Here
Final Thought
The Node.js OS module is a handy tool when you need to fetch system-level information directly in your application. Whether you are building monitoring tools, optimizing resource usage, or simply learning how your system interacts with Node.js, these methods provide a solid foundation.
Stay tuned to UpdateGadh for more practical Node.js tutorials.
Complete Advance AI topics:- CLICK HERE
Deep Learning Tutorial:– Click Here
path module in node js
util module in node js
node os
os.availableparallelism is not a function
file system module in node js
crypto module in node js
os module in python
cannot find module ‘node:os’
node js os module w3schools
node js os module tutorial
node js os module example
node js os module list
os availableparallelism is not a function
Post Comment