Node.js | os.platform() Method

The os.platform() method is an inbuilt application programming interface of the os module which is used to get the Operating system platform.

Syntax:

os.platform()

Parameters: This method does not accept any parameters.

Return Value: This method returns a string that represents operating system platform. The returned values can be one of these ‘aix’, ‘android’, ‘darwin’, ‘freebsd’, ‘linux’, ‘openbsd’, ‘sunos’, and ‘win32’. This values is set at compile time.

Below examples illustrate the use of os.platform() method in Node.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// os.platform() method 
     
// Require os module
const os = require('os');
  
// Printing os.platform() value
console.log(os.platform());

chevron_right


Output:

linux

Example 2: This example is the alternative of first example.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// os.platform() method
console.log(process.platform);

chevron_right


Output:

linux

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the    
// os.platform() method 
     
// Require os module
const os = require('os');
  
// Printing os.platform() value
var platform = os.platform();
  
switch(platform) {
    case 'aix': console.log("IBM AIX platform");
        break;
    case 'android': console.log("Android platform");
        break;
    case 'darwin': console.log("Darwin platfrom(MacOS, IOS etc)");
        break;
    case 'freebsd': console.log("FreeBSD Platform");
        break;
    case 'linux': console.log("Linux Platform");
        break;
    case 'openbsd': console.log("OpenBSD platform");
        break;
    case 'sunos': console.log("SunOS platform");
        break;
    case 'win32': console.log("windows platform");
        break;    
    default: console.log("unknown platform");
}

chevron_right


Output:

Linux Platform

Note: The above program will compile and run by using the node index.js command.

Reference: https://nodejs.org/api/os.html#os_os_platform




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.