How do I determine the current operating system with Node.js


Question

I'm writing a couple of node shell scripts for use when developing on a platform. We have both Mac and Windows developers. Is there a variable I can check for in Node to run a .sh file in one instance and .bat in another?

1
221
3/7/2015 12:29:55 AM

Accepted Answer

The variable to use would be process.platform

On Mac the variable returns darwin. On Windows, it returns win32 (even on 64 bit).

Possible values are: 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'

I just set this at the top of my jakeFile:

var isWin = process.platform === "win32";
402
2/4/2018 2:38:21 AM

You are looking for the OS native module for Node.js:

v4: https://nodejs.org/dist/latest-v4.x/docs/api/os.html#os_os_platform

or v5 : https://nodejs.org/dist/latest-v5.x/docs/api/os.html#os_os_platform

os.platform()

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon