Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Modules are the blocks of code encapsulated and communicated with an external application based on its related functionalities.
The Node.js os module contains utility methods and properties for retrieving information and interacting with the Operating System and computer on which the program is running.
There are a few valuable properties that tell us important information about how to handle files in the Node.js os module:
os.EOL
It provides a sequence of line delimiters.
\n on POSIX (Portable Operating System Interface)
\r\n on Windows
os.constants
It returns an object containing the operating system specified constants. Constants for error codes, process signals, and other operating system-specific issues are included.
Some of the constants are:
SIGTRAP - It is sent to a process when an exception occurs.
SIGCHLD - It is sent to a process after a child process terminates.
You can explore the complete list of OS constants here.
os.devNull
It is a platform-specific file path of the null device.
\\.\nul on Windows
/dev/null on POSIX
Methods of Node.js os module
Let us look at some of the methods that Node.js os module has in stock.
os.getPriority([pid])
The os.getPriority() method accepts a single optional parameter pid. pid specifies the process id whose scheduling priority we need to get. The default value of pid is 0. This method returns the integer value that specifies the scheduling priority of the process we specified using the pid. If the pid is 0, then the scheduling priority of the current process is returned
os.arch()
Returns the string value of the CPU architecture of the operating system for which the Node.js binary was compiled. Some of the possible values are:
'x64', 'arm', 'ia32', 'mips', 'mipsel', 'ppc64', 's390', and 'x32'.
The value that is returned is the same as the process.arch.
os.cpus()
It returns an object array with information on each logical CPU core.
Each object has the following properties:
model <string>
speed <number> (in MHz)
times <Object>
user <number> The number of milliseconds the CPU has spent in the user mode.
nice <number> The number of milliseconds the CPU has spent in the nice mode.
sys <number> The number of milliseconds the CPU has spent in the sys mode.
idle <number> The number of milliseconds the CPU has spent in the idle mode.
irq <number> The number of milliseconds the CPU has spent in the irq mode.
Nice values are only specific to POSIX. For Windows, nice values for all processors are always 0.
Note: Nice values are user-space values that can be used to control the priority of a process.
The endianness of the CPU on which the Node.js binary was compiled is returned as a string. Big-endian ('BE') and small endian ('LE') are two possible values.
Note: Endianness refers to the order or sequence of bytes of a word of digital data in the computer memory.
os.freemem()
Return the number of bytes representing the system's free memory.
os.homedir()
The string path to the current user's home directory is returned.
If the $HOME environment variable is defined on POSIX, it is used. Otherwise, it looks up the user's home directory using the effective UID.
If the USERPROFILE environment variable is set on Windows, it is used. Otherwise, it uses the current user's profile directory's path.
os.hostname()
The host name of the operating system's owner is returned as a string.
os.loadavg()
Returns an array providing the load averages for 1, 5, and 15 minutes.
The load average is a fractional number that the operating system calculates as a measure of system activity.
The load average is a concept unique to Unix. The return value on Windows is always [0, 0, 0].
os.networkInterfaces()
Returns information about the network interfaces on your system.
The following properties are available on the assigned network address object:
address <string> The assigned IPv6 or IPv4 address
netmask <string> The IPv6 or IPv4 network mask
family <string> Either IPv6 or IPv4
mac <string> The MAC address of the network interface
Internal <boolean> If the network interface is a loopback or equivalent non-remotely accessible interface, true; otherwise, false.
scopeid <number> The numeric IPv6 scope ID (only specified when the family is IPv6)
cidr <string> The assigned IPv6 or IPv4 address in CIDR(Classless Inter-Domain Routing) notation with the routing prefix. If the netmask is invalid, then this property is set to null.
The os.platform() returns a string that identifies the operating system platform the system is running on.
Possible values are
Aix
Darwin
FreeBSD
Linux
OpenBSD
SunOS
win32
os.release()
The operating system release number is returned as a string.
os.tmpdir()
As a string, this method returns the operating system's default temporary file directory.
os.totalmem()
As an integer, it returns the total amount of system memory in bytes.
os.type()
Returns the operating system name.
Example:
'Darwin' on macOS, 'Windows_NT' on Windows, etc.
os.uptime()
This function returns the number of seconds it has been running since the machine was last rebooted.
os.userInfo()
The os.userinfo() returns the current username, uid (), gid (), shell, and homedir are all returned as an object.)
This is usually a subset of the password file on POSIX platforms. The username, uid, gid, shell, and homedir are all included in the returned object. The uid and gid values for Windows are -1, and the shell is null.
os.version()
The kernel version is returned as a string.
Uname(3) function is used to determine the operating system release on PosIX platforms. RtlGetVersion() is used on Windows, and GetVersionExW() is used if RtlGetVersion() is not accessible.
Let's see the output of some of these properties and methods:
// Including the OS module and creating its object
var JS_os = require('os');
// Returns the total amount of system memory
// Return data type is bytes
console.log("The total memory in system is: " + JS_os.totalmem() + " bytes.");
// Returns the CPU architecture
console.log("The CPU architecture is: " + JS_os.arch());
// Returns the amount of free system memory
// Return data type is bytes
console.log("Free memory available: " + JS_os.freemem() + " bytes.");
// It returns the operating systems default directory for temp files.
console.log('OS default directory for temp files : ' + JS_os.tmpdir ());
// Returns the operating system name
console.log("Operating system name: " + JS_os.type());
// Returns the platform of the OS
console.log('operating system platform is: ' + JS_os.platform());
// Returns the operating systems release.
console.log('OS release: ' + JS_os.release());
// It returns the hostname of the system
console.log("Hostname of system: " + JS_os.hostname());
// Returns the endianness of the system
console.log("Endianness of system is: " + JS_os.endianness());
Output
Frequently Asked Questions
1. What is the Node.js os module?
The Node.js os module provides several functions for interacting with the operating system. It's also used to provide information about the operating system on the computer.
2. How do we know the hostname of the operating system in Node.js?
You can use the os.hostname() to get the operating system's hostname in Node.js.
3. How can we know the amount of free memory in Node.js?
You can use os.freemem() to know the amount of free memory in your system.
Key Takeaways
The Node.js os module contains utility methods and attributes for accessing information and dealing with the operating system and computer that the program is running on. The functions available inside this module can help you extract valuable data about the system like hostname, free memory, and many other things.