Last updated: Apr 5, 2024
Reading timeยท4 min
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.
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
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.
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.
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 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.
If you need to check if the current operating system is Windows with Node.js:
os.platform()
method to get the operating system.win32
.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()); }
If you have to do this often, extract the logic into a reusable function.
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.
The same approach can be used to check if the operating system is macOS using Node.
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()); }
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.
You can use the same approach if you need to check if the operating system is Linux.
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()); }
If the operating system is Linux, the if
block runs, otherwise, the else
block runs.
process.platform
property to get the operating system with Node.jsYou can also use the process.platform
property to get the operating system in
Node.js.
import process from 'process'; // ๐๏ธ if you use CommonJS require() // const process = require('process'); console.log(process.platform); // ๐๏ธ linux
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
.
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); }
And here is a conditional that checks if the operating system is Windows, macOS or Linux.
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 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.
You can learn more about the related topics by checking out the following tutorials: