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?
-
Like combining module _Child Processes_ with module _OS_? – Wolfgang Kuehn Dec 30 '11 at 20:49
10 Answers
The variable to use would be process.platform
On Mac the variable is set to darwin
. On Windows, it is set to win32
(even on 64 bit).
aix
darwin
freebsd
linux
openbsd
sunos
win32
android
(Experimental, according to the link)
I just set this at the top of my jakeFile:
var isWin = process.platform === "win32";

- 1
- 1

- 40,827
- 17
- 81
- 86
-
9You should use the OS module better, it's even in the documentation. os.platform specifically – alessioalex Dec 30 '11 at 20:58
-
120windows returns `'win32'`, even on 64 bit operation systems. i.e. `process.platform === 'win32'` is sufficient – Raynos Dec 30 '11 at 21:44
-
27I checked os.platform and process.platform and they are the exact same on window and mac. I will use process.platform since it doesn't require including a lib. – Mauvis Ledford Jan 06 '12 at 08:47
-
can I run this code without creating a js file? Ie **node "some inline script"** – SuperUberDuper Dec 08 '15 at 11:25
-
4
-
12While the string compare of `process.platform === 'win32'` seems more concise than the regex, and obviously quicker. The regex Mauvis has posted seems to be a better _quality_ check. If Node/Windows every decided to return win64, winARM, etc., or anything else a ton of code would break with the string compare. All we are looking for is if it's Windows or not. Not the arch. I vote for the regex at this time after having used the string compare for a while, the regex 'feels' safer. – Andrew T Finnell Oct 04 '16 at 13:56
-
18@AndrewTFinnell: Win32 does not actually mean "Windows on x86"—Windows on x64, ARM, or even Itanium are all Win32, because Win32 is the name of the API regardless of what processor it is running on. So the architecture has nothing to do with the name Win32. I would argue that the regex check is more dangerous, because some other, *incompatible* platform might conceivably start with the characters "win" at some point in the future. – Dietrich Epp Feb 25 '17 at 00:16
-
2@JF Should actually be `node -p process.platform` without the quotes. With the quotes, it just returns the string "process.platform" – shiser Mar 09 '17 at 02:18
-
@shiser I just tried it in the macOS terminal, and it works just fine. What OS are you on? – Jed Fox Mar 09 '17 at 11:07
-
Just tried again to ensure I'm not imagining things. Running Node 6.9.4 on Windows (10, 64-bit) – shiser Mar 11 '17 at 23:45
-
3"If Node/Windows every decided to return win64, winARM, etc., or anything else a ton of code would break with the string compare" <-- I don’t think it’s a good justification for a regex. Node knows perfectly well that this would break every other library so they’d never do a breaking change like this. – Dan Abramov Jan 16 '18 at 15:47
-
1To get the architecture, you can run `process.arch`. It will return, for example on Windows, either `x86` or `x64`. – a_rahmanshah Oct 13 '18 at 08:17
With Node.js v6 (and above) there is a dedicated os
module, which provides a number of operating system-related utility methods.
On my Windows 10 machine it reports the following:
var os = require('os');
console.log(os.type()); // "Windows_NT"
console.log(os.release()); // "10.0.14393"
console.log(os.platform()); // "win32"
You can read it's full documentation here: https://nodejs.org/api/os.html#os_os_type

- 618
- 1
- 11
- 23

- 51,456
- 28
- 233
- 198
-
3This works, but only for server side code execution, it does not give the platform of the client code. Only the platform of the server executing the code. I tried it with BrowserStack and it only gives the operating system of the Docker Container instance that is the runner, not the browser instance running on BrowserStack, which could be iOS or Android, or Windows or OSX. – Seth Eden Aug 27 '18 at 19:30
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.

- 3,429
- 2
- 24
- 35

- 62,577
- 16
- 155
- 122
-
10
-
6process.platform works too, without a require: https://nodejs.org/dist/latest-v4.x/docs/api/process.html#process_process_platform – David Braun Jan 19 '16 at 03:03
-
-
Process
var opsys = process.platform;
if (opsys == "darwin") {
opsys = "MacOS";
} else if (opsys == "win32" || opsys == "win64") {
opsys = "Windows";
} else if (opsys == "linux") {
opsys = "Linux";
}
console.log(opsys) // I don't know what linux is.
OS
const os = require("os"); // Comes with node.js
console.log(os.type());

- 6,997
- 13
- 68
- 80

- 929
- 12
- 5
-
17**There is no platform called "win64"**. You can find the available values here: https://nodejs.org/api/process.html#process_process_platform Also, you forgot to close your last *else if*. – frzsombor Apr 26 '19 at 19:10
-
4`os.type()` seems to return 'Darwin' with an uppercase. For safety reasons it might be better to call `os.type().toLowerCase()`before comparing values. – cwouter Jul 01 '19 at 12:44
This Works fine for me
var osvar = process.platform;
if (osvar == 'darwin') {
console.log("you are on a mac os");
}else if(osvar == 'win32'){
console.log("you are on a windows os")
}else{
console.log("unknown os")
}

- 2,104
- 1
- 19
- 28
Works fine for me
if (/^win/i.test(process.platform)) {
// TODO: Windows
} else {
// TODO: Linux, Mac or something else
}
The i modifier is used to perform case-insensitive matching.

- 7,997
- 3
- 56
- 43
when you are using 32bits node on 64bits windows(like node-webkit or atom-shell developers), process.platform will echo win32
use
function isOSWin64() {
return process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
}
(check here for details)

- 55
- 2
I was facing the same issue running my node js code on Windows VM on mac machine. The following code did the trick.
Replace
process.platform == 'win32'
with
const os = require('os');
os.platform() == 'win32';

- 1
- 1

- 1,813
- 15
- 19
const path = require('path');
if (path.sep === "\\") {
console.log("Windows");
} else {
console.log("Not Windows");
}

- 95
- 1
- 4
-
5While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 07 '17 at 14:01
-
Even though that would work in most cases, I would prefer using the `os` module, which is more expressive when reading the code. – Stephan Feb 03 '18 at 22:10