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?
10 Answers
The variable to use would be process.platform
On Mac the variable returns darwin. On Windows, it returns win32 (even on 64 bit).
aixdarwinfreebsdlinuxopenbsdsunoswin32android(Experimental, according to the link)
I just set this at the top of my jakeFile:
var isWin = process.platform === "win32";
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
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.
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());
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)