NodeJS How to determine or check for current OS (Operating System)

There are two ways of doing it,

First using the native OS module,

Check out the API function available for OS module here 

Basically, all you have to do is to call platform() function from the OS class. Here is how its done

Check if its windows operating system,


Returns the operating system platform. Possible values are,'darwin' 'freebsd''linux''sunos' or.'win32'

A bit detailed code version with some other methods from my Windows 10 machine

Second, using the process’s platform property,

os.platform() basically returns the values of process.platform property

Above logic can be rewritten as

I hope this helps


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.