• Node.js Video Tutorials

Node.js - os.type() Method



The Node.js os.type() method returns a string value that specifies the operating system type. For example, the returned string can be 'Linux' on Linux operating system, 'Darwin' on the mac operating system, and 'Windows_NT' on the windows operating system.

Syntax

Following is the syntax of the Node.js os.type() method −

os.type()

Parameters

This method does not accept any parameters.

Return value

This method returns a string that indicates the type of the operating system.

Example

In the following example, we are trying to return the type of the current operating system by logging the Node.js os.type() method to the console.

const os = require('os');
console.log(os.type());

Output

Linux

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the os.type() method returned the type of the current operating system as shown in the output below.

Windows_NT

Example

In the example below,

  • We performed a switch case to get the type of the operating system.

  • So the switch checks each case against the output string value of the os.type() method until a match is found.

  • If nothing matches, the default condition will be printed.

const os = require('os');
const type_of_OS = os.type();
switch(type_of_OS) {
   case 'Linux':
      console.log("Hi, i'm Linux operating system. Ok bye");
      break;
   case 'Darwin':
      console.log("Hi, i'm Darwin operating system. Ok bye");
      break;
   case 'Windows_NT':
      console.log("Hi, i'm windows_NT operating system. Ok bye");
      break;
}

Output

Hi, i'm Linux operating system. Ok bye

Note − To get the accurate result, better execute the above code in local.

When we compile and run the above program, the output string value of the os.type() method will be 'Windows_NT'. So the case 'Windows_NT' got matched and executed.

Hi, i'm windows_NT operating system. Ok bye
nodejs_os_module.htm
Advertisements