• Node.js Video Tutorials

Node.js - os.platform() Method



The Node.js os.platform() method returns a string value that specifies the operating system platform of the system that compiled the Node.js binary. The possible output string values are 'aix', 'darwin', 'freebsd', 'linux', 'openbsd', 'sunos', or 'win32'. The value 'android' may be returned when the Node.js binary is compiled on an Android device. However this 'Android' is an experimental phase in Node.js.

The process.platform property can also get the operating system platform of the current system.

Syntax

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

os.platform()

Parameters

This method does not accept any parameters.

Return value

This method returns a string value that specifies the operating system platform of the system where the Node.js binary is compiled.

Example

In the following example, we are trying to get the operating system platform of the current system by using the Node.js os.platform() method.

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

Output

linux

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

If we compile and run the above program, the os.platform() method returned the operating system platform

win32

Example

In the following example, we are trying to get the operating system platform of the current system by using the process.platform property of Node.js.

console.log(process.platform);

Output

linux

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

After executing the above program, the process.platform property printed the operating system platform.

win32

Example

In the example below,

  • We are performing a switch case to get the operating system platform of the current system.

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

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

const os = require('os');
const Ope_sys_platform = os.platform()
switch(Ope_sys_platform){
   case 'aix':
      console.log('This is IBM AIX platform');
      break;
   case 'darwin':
      console.log('This is Darwin platform');
      break;
   case 'freebsd':
      console.log('This is FreeBSD Platform');
      break;
   case 'linux':
      console.log('This is Linux Platform');
      break;
   case 'openbsd':
      console.log('This is OpenBSD platform');
      break;
   case 'sunos':
      console.log('This is SunOS platform');
      break;
   case 'win32':
      console.log('This is windows platform');
      break;
   case 'android':
      console.log('This is Android platform');
      break;
   default:
      console.log('This is an Unknown platform');
}

Output

This is Linux Platform

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.platform() method will be 'win32'. So the case 'win32' got matched and executed.

This is windows platform
nodejs_os_module.htm
Advertisements