Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

os.platform() returns darwin instead of OSX

os.platform();

The above JS instruction returns the name of OS .

  • When it is runned on Ubuntu , it returns

    'linux'
    
  • When it is runned on Macbook, it returns

    'darwin'
    

I am wondered why does not return osx ,unix or bsd.. ?

Does darwinis a fork of osx ?

How to get the type of OS under MAC using Node.js ?

like image 928
Abdennour TOUMI Avatar asked Dec 07 '22 21:12

Abdennour TOUMI


2 Answers

Darwin is the underlying platform for OS X.

To get the OS X version instead, you can do this via the command line (or child process) with: defaults read loginwindow SystemVersionStampAsString or sw_vers -productVersion

To get the version via C/C++ (which you could write a binding for to access from node):

// compile with: g++ osx_ver.cc -I/Developer/Headers/FlatCarbon -framework CoreServices
#include <Gestalt.h>
#include <stdio.h>

int main() {
  SInt32 majorVersion, minorVersion, bugFixVersion;

  Gestalt(gestaltSystemVersionMajor, &majorVersion);
  Gestalt(gestaltSystemVersionMinor, &minorVersion);
  Gestalt(gestaltSystemVersionBugFix, &bugFixVersion);

  printf("%d.%d.%d", majorVersion, minorVersion, bugFixVersion);

  return 0;
}

Note: The Gestalt() usage shown above is deprecated since OS X 10.8, but its replacement seemingly isn't available until OS X 10.10, so you may need to use Objective-C instead ([processInfo operatingSystemVersion]) and branch on API availability like what is done here in Chromium.

like image 30
mscdex Avatar answered Dec 25 '22 20:12

mscdex


Darwin is not OSX, OSX is Darwin.

Darwin is the core of OSX, in the same way, Linux is the core of Debian, Ubuntu, and every other Linux distributions.

So while you can be 100% sure that any Apple product (macOS, OSX, iOS, watchOS, tvOS) will return Darwin, it is technically not correct to assume that darwin will only ever be thoses OSs, since some low profile open source projects exists, like PureDarwin, and thoses would also return darwin.

However, I do not know of any (officials) port of node.js for any other Darwin-based OSs than OSX, so in practice, yes, if you get darwin, you can be confident it is OSX.

like image 75
DrakaSAN Avatar answered Dec 25 '22 22:12

DrakaSAN