How to get the current Operating System using Node.js

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
4 min

banner

# Table of Contents

  1. How to get the current Operating System using Node.js
  2. Check if the current Operating system is Windows using Node.js
  3. Check if the current Operating system is macOS using Node.js
  4. Check if the current Operating system is Linux using Node.js
  5. Using the process.platform property to get the operating system with Node.js

# How to get the current Operating System using Node.js

Use the os.platform() method to get the current operating system with Node.js.

The method returns a string that identifies the operating system platform for which the Node.js binary was compiled.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') console.log(os.platform()); // ๐Ÿ‘‰๏ธ linux console.log(os.type()); // ๐Ÿ‘‰๏ธ Linux console.log(os.release()); // ๐Ÿ‘‰๏ธ 5.19.0-41-generic

get current operating system with node js

The code for this article is available on GitHub

The os.platform() method returns the following possible values:

  • aix
  • darwin (macOS)
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32 (Windows)

The string android might also be returned if Node.js was built on the Android operating system. However, note that Android support in Node.js is experimental.

The os.release() method returns the operating system as a string.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') os.platform(); // ๐Ÿ‘‰๏ธ 'darwin' os.release(); // ๐Ÿ‘‰๏ธ '22.4.0'

On POSIX systems, the operating system release is determined by calling uname and on Windows, the GetVersionExW() method i used.

Here is a conditional that checks whether the operating system is Windows, macOS or Linux.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') const operatingSystem = os.platform(); if (operatingSystem === 'win32') { console.log('The operating system is Windows'); } else if (operatingSystem === 'darwin') { console.log('The operating system is macOS'); } else if (operatingSystem === 'linux') { console.log('The operating system is Linux'); }
The code for this article is available on GitHub

The if statement checks if the operating system is Windows.

The first else if statement checks if the operating system is macOS.

The second else if statement checks if the operating system is Linux.

# Check if the current Operating system is Windows using Node.js

If you need to check if the current operating system is Windows with Node.js:

  1. Use the os.platform() method to get the operating system.
  2. Compare the method's return value to win32.
  3. If the condition is met, the operating system is Windows.
index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') if (os.platform() === 'win32') { console.log('The operating system is Windows'); console.log(os.platform()); } else { console.log('The operating system is NOT Windows'); console.log(os.platform()); }

check if the operating system is windows using node js

The code for this article is available on GitHub

If you have to do this often, extract the logic into a reusable function.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') function isWindows() { return os.platform() === 'win32'; } if (isWindows()) { console.log('The operating system is Windows'); console.log(os.platform()); } else { console.log('The operating system is NOT Windows'); console.log(os.platform()); }

If the current operating system is Windows, the if block runs, otherwise, the else block runs.

# Check if the current Operating system is macOS using Node.js

The same approach can be used to check if the operating system is macOS using Node.

index.js
import os from 'os'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const os = require('os') function isMacOS() { return os.platform() === 'darwin'; } if (isMacOS()) { console.log('The operating system is macOS'); console.log(os.platform()); } else { console.log('The operating system is NOT macOS'); console.log(os.platform()); }
The code for this article is available on GitHub

We called the os.platform() method and compared the result to the string darwin.

Darwin is the core Unix operating system of macOS.

If the operating system is macOS, the if block runs, otherwise, the else block runs.

# Check if the current Operating system is Linux using Node.js

You can use the same approach if you need to check if the operating system is Linux.

index.js
import os from 'os'; function isLinux() { return os.platform() === 'linux'; } if (isLinux()) { console.log('The operating system is Linux'); console.log(os.platform()); } else { console.log('The operating system is NOT Linux'); console.log(os.platform()); }

check if operating system is linux

The code for this article is available on GitHub

If the operating system is Linux, the if block runs, otherwise, the else block runs.

# Using the process.platform property to get the operating system with Node.js

You can also use the process.platform property to get the operating system in Node.js.

index.js
import process from 'process'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const process = require('process'); console.log(process.platform); // ๐Ÿ‘‰๏ธ linux

get operating system in node using process platform

Notice that process.platform is a property and not a method.

The process.platform property returns a string that identifies the operating system platform for which the Node.js binary was compiled.

The property returns the same values as the os.platform() method:

  • aix
  • darwin (macOS)
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32 (Windows)

You can use the following code sample if you need to check if the operating system is Windows using process.platform.

index.js
import process from 'process'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const process = require('process'); if (process.platform === 'win32') { console.log('The operating system is Windows'); console.log(process.platform); } else { console.log('The operating system is NOT Windows'); console.log(process.platform); }

check if operating system is windows using process platform

And here is a conditional that checks if the operating system is Windows, macOS or Linux.

index.js
import process from 'process'; // ๐Ÿ‘‡๏ธ if you use CommonJS require() // const process = require('process'); const operatingSystem = process.platform; if (operatingSystem === 'win32') { console.log('The operating system is Windows'); } else if (operatingSystem === 'darwin') { console.log('The operating system is macOS'); } else if (operatingSystem === 'linux') { console.log('The operating system is Linux'); }
The code for this article is available on GitHub

The if statement checks if the operating system is Windows.

The first else if statement checks if the operating system is macOS.

The second else if statement checks if the operating system is Linux.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev