Home >>Nodejs Tutorial >Node.js OS

Node.js OS

Node.js OS

Node.js OS provides some essential utility functions related to the operating

system. Let's see the list of functions or methods which are generally used.

Index Method Description
1. os.arch() This method is used to retrieve the CPU architecture of the operating system.
2. os.cpus() This method is used to fetch an array of objects that contain information about each installed cpu / core: model, speed (in MHz), and times (an object that contains the number of milliseconds the cpu / core spent in: user, idle, sys, nice, and irq)
3. os.endianness() That method returns the cpu's endianness. Its potential values are 'BE' for large endian, or 'LE' for small endian.
4. os.freemem() Such methods return in Bytes the sum of free device memory.
5. os.homedir() This method returns the current User's home tab.
6. os.hostname() This method is used to return operating system hostname.
7. os.loadavg() This approach returns an array comprising the load averages of 1, 5 and 15 minutes. The load average is a device operation time fraction, determined by the operating system and expressed as a fractional number.
8. os.networkinterfaces() This approach returns a list of interfaces at the network.
9. os.platform() This method returns the running computer's operating system platform i.e. darwin, 'win32', 'freebsd,' 'linux,' 'sunos' etc.
10. os.release() This method returns the release of the operating system.
11. os.tmpdir() This approach returns the default directory for temporary files within the operating system.
12. os.totalmem() This method returns in Bytes the total amount of system memory.
13. os.type() The method returns the name of the operating system. For example, on linux, darwin on os x, and windowsnt.
14. os.uptime() This method returns uptime for the machine in seconds.
15. os.userinfo([options]) This method returns a password file entry sub-set to the current user in consequence.
Node.js OS Example 1

We're including some core functions in this example. Build a file called os example1.js that has the code:

var os = require("os");
// Endianness
console.log('endianness : ' + os.endianness());
// OS type
console.log('type : ' + os.type());
// OS platform
console.log('platform : ' + os.platform());
// Total system memory
console.log('total memory : ' + os.totalmem() + " bytes.");
// Total free memory
console.log('free memory : ' + os.freemem() + " bytes.");

Now run the os_example1.js to see the result−

$ node os_example1.js
Verify the Output.
endianness : LE
type : Linux
platform : linux
total memory : 25103400960 bytes.
free memory : 20676710400 bytes