w3resource

Node.js : OS Module

Introduction

The node.js "os" module provides a few basic operating-system related utility functions. To access these functions use require('os'). Following functions are in the module 'os'.

Contents:

os.tmpdir()

The function is used to get the operating system's default directory for temp files.

Example :

C:\Users\workarea>node
> os.tmpdir()
'C:\\Users\\workarea\\AppData\\Local\\Temp'

os.endianness()

The function is used to get the endianness of the CPU. Possible values are "BE" or "LE".+

Note : According to Wikipedia the terms endian and endianness refer to the convention used to interpret the bytes making up a data word when those bytes are stored in computer memory.

Example :

> os.endianness()
'LE'
>

os.hostname()

The function is used to get the hostname of the operating system.

Example :

> os.hostname()
'workarea-PC'
>

os.type()

The function is used to get the operating system name.

Example :

> os.type()
'Windows_NT'
>

os.platform()

The function is used to get the operating system platform.

Examples :

> os.platform()
'win32'

os.arch()

The function is used to get the operating system CPU architecture.

Example :

> os.arch() 
'ia32'

os.release()

The function returns the operating system release.

Example :

> os.release()
'6.1.7600'

os.uptime()

The function returns the system uptime in seconds.

Example :

> os.uptime()
20218.5031917

os.loadavg()

The function returns an array containing the 1, 5, and 15 minute load averages.

  • As a rule of thumb, the load average should ideally be less than the number of logical CPUs in the system.
  • The load average is a very UNIX-y concept.
  • There is no real equivalent on Windows platforms. That is why this function always returns [0, 0, 0] on Windows.

Example (Windows) :

> os.loadavg() 
[ 0, 0, 0 ]

Example (Ubuntu LTS 14.04 (Kernel version 3.13.0-24-generic) :

> os.loadavg()
[ 3.06396484375, 1.7939453125, 0.7080078125 ]

os.totalmem()

The function is used to get the total amount of system memory in bytes.

Example :

> os.totalmem() 
3210207232

os.freemem()

The function is used to get the amount of free system memory in bytes.

Example :

> os.freemem() 
1521180672

os.cpus()

The function returns an array of objects containing information about each CPU/core installed: model, speed (in MHz), and times.

Note : An object containing the number of milliseconds the CPU/core spent in: user, nice, sys, idle, and irq.

Examples :

> os.cpus()
[ { model: 'Pentium(R) Dual-Core  CPU      E5200  @ 2.50GHz',
    speed: 2500,
    times:
       { user: 2630130,
	     nice: 0,
		 sys: 1139743,
		 idle: 19108656,
		 irq: 19952 } },
   { model: 'Pentium(R) Dual-Core  CPU      E5200  @ 2.50GHz',
         speed: 2500,
		 times:
	   { user: 2803618,
	     nice: 0,
		 sys: 1047515,
         idle: 19025788,
		 irq: 25459 } } ]

os.networkInterfaces()

The function is used to get the list of network interfaces.

Example :


> os.networkInterfaces()
{ 'Local Area Connection 5':
   [ { address: 'fe80::e159:a5d6:ce2a:90b6',
       family: 'IPv6',
	   internal: false },
	 { address: '192.168.0.100',
	   family: 'IPv4',
	   internal: false } ],
  'Loopback Pseudo-Interface 1':
    [ { address: '::1',
	    family: 'IPv6',
		internal: true },
      { address: '127.0.0.1',
	   family: 'IPv4',
	   internal: true } ],
  'Teredo Tunneling Pseudo-Interface':
    [ { address: '2001:0:9d38:6abd:281a:2050:3f57:ff9b',
	    family: 'IPv6',
		internal: false },
      { address: 'fe80::281a:2050:3f57:ff9b',
	    family: 'IPv6',
		internal: false } ] }

os.EOL

A constant defining the appropriate End-of-line marker for the operating system.

Example :

gt; os.EOL
'\r\n'

Previous: Buffer
Next: Database Node-MySQL



Follow us on Facebook and Twitter for latest update.