Nodejs – OS module

Nodejs OS module provides basic operating-system related utility functions and properties of the computer’s system operating system. You can import this module using the following syntax.

SYNTAX

The syntax to include the Nodejs OS module in your application:

var os = require('os');

Nodejs – OS module

Some useful points related to handling files in Nodejs OS module: 

  1. os.EOL gives the line delimiter sequence. It’s \n on Linux and macOS, and \r\n on Windows.
  2. os.constants.errno sets the constants for error reporting, like EADDRINUSE, EOVERFLOW and more.

MODULE properties and Methods

The Nodejs OS module possesses many properties and uses various methods to provide information related to utility functions.

S.NO METHODS DESCRIPTION
1 os.tmpdir() Returns the default directory of an operating system for temp files
2 constants Returns an object containing constants of an operating system for process signals, error codes etc.
3 cpus() Returns an array containing information about the CPU of the computer
4 endiannes() Returns the endianness of the CPU
5 EOL Returns the current operating system’s end-of-line marker for
6 freemem() Returns the number of free memory of the system
7 hostname() Returns the hostname of the operating system
8 arch() Returns the operating system CPU architecture
9 networkInterfaces() Returns the network interfaces with a network address
10 platform() Returns information about the operating system’s platform
11 release() Returns information about the operating system’s release
12 totalmem() Returns total amount of memory in the system
13 type() Returns the operating system’s name
14 uptime() Returns the operating system’s uptime, in seconds
15 userInfo() Returns the current user’s information

Advantages:

  • It enables users to interact with the operating system.
  • And, Provides the hostname of the operating system.
  • Then, Returns the free system memory in bytes.

Example 1:

// Include os module and create its object 

var os = require('os'); 


// return the cpu architecture 

console.log("CPU architecture: " + os.arch()); 


// returns the amount of free system memory in bytes 

console.log("Free memory: " + os.freemem()); 


/ return total amount of system memory in bytes

console.log("Total memory: " + os.totalmem()); 


// returns the list of network interfaces 

console.log('List of network Interfaces: ' + 

os.networkInterfaces()); 


// returns the operating systems default directory for temp files. 

console.log('OS default directory for temp files : ' + 

os.tmpdir ()); 

Output:

D:\ node>node a.js

CPU architecture: x64

Free memory: 1021123448

Total memory: 4198723026

List of network Interfaces: [object Object]

OS default directory for temp files : 

C:\User\Hari\AppData\Local\Temp

D: \node> 

Read: Node.js – Packages

Example 2:

// Include os module and create its object 

var os = require('os'); 


// return the endianness of system 

console.log("Endianness of system: " + os.endianness()); 


// returns hostname of system 

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


// return operating system name 

console.log("Operating system name: " + os.type()); 


// returns the platform of os 

console.log('operating system platform: ' + os.platform()); 


// returns the operating systems release. 

console.log('OS release : ' + os.release()); 

Output:

D:\node>node a.js

Endianess of system: LE

Hostname: LAPTOP-E6540

Operating system name: Windows_10

Operating system platform: win64

OS release : 10.0.17134

CONCLUSION

We hope this knowledge base fed you with enough information that you’re searching for.

Contact us to know more about the Nodejs and its ability in Web development.

Last Update: April 17, 2020  

April 17, 2020   135   Haribabu    Nodejs, Nodejs Tutorial    
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Twitter
INSTAGRAM
LinkedIn