如何使用Node.js确定当前的操作系统

时间:2022-08-16 03:35:40

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?

我正在编写一些节点shell脚本,以便在平台上开发时使用。我们有Mac和Windows开发人员。是否有一个变量可以在Node中检查,以便在一个实例中运行.sh文件,在另一个实例中运行.bat ?

8 个解决方案

#1


296  

The variable to use would be process.platform

要使用的变量是process.platform

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

在Mac上,变量返回达尔文。在Windows上,它返回win32(甚至64位)。

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

可能的值有:“darwin”、“freebsd”、“linux”、“sunos”或“win32”

I just set this at the top of my jakeFile:

我把它放在我的jakeFile的顶部:

var isWin = process.platform === "win32";

#2


40  

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

您正在为Node.js寻找OS本机模块:

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

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

或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.

返回操作系统平台。可能的值是“达尔文”、“freebsd”、“linux”、“sunos”或“win32”。返回process.platform的值。

#3


25  

With Node.js v6 (and above) there is a dedicated os module, which provides a number of operating system-related utility methods.

与节点。有一个专用的os模块,它提供了许多与操作系统相关的实用工具方法。

On my Windows 10 machine it reports the following:

在我的Windows 10机器上,它报告如下:

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

您可以在这里阅读它的完整文档:https://nodejs.org/api/os.html

#4


4  

when you are using 32bits node on 64bits windows(like node-webkit or atom-shell developers), process.platform will echo win32

当您在64位窗口(如node-webkit或atom-shell开发人员)上使用32位节点时,请进行处理。平台将回声win32

use

使用

    function isOSWin64() {
      return process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
    }

(check here for details)

(检查)

#5


1  

var isWin64 = process.env.hasOwnProperty('ProgramFiles(x86)');

#6


1  

const path = require('path');

if (path.sep === "\\") {
console.log("Windows");
} else {
console.log("Not Windows");
}

#7


1  

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.

i修饰符用于执行不区分大小写的匹配。

#8


0  

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")
}

#1


296  

The variable to use would be process.platform

要使用的变量是process.platform

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

在Mac上,变量返回达尔文。在Windows上,它返回win32(甚至64位)。

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

可能的值有:“darwin”、“freebsd”、“linux”、“sunos”或“win32”

I just set this at the top of my jakeFile:

我把它放在我的jakeFile的顶部:

var isWin = process.platform === "win32";

#2


40  

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

您正在为Node.js寻找OS本机模块:

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

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

或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.

返回操作系统平台。可能的值是“达尔文”、“freebsd”、“linux”、“sunos”或“win32”。返回process.platform的值。

#3


25  

With Node.js v6 (and above) there is a dedicated os module, which provides a number of operating system-related utility methods.

与节点。有一个专用的os模块,它提供了许多与操作系统相关的实用工具方法。

On my Windows 10 machine it reports the following:

在我的Windows 10机器上,它报告如下:

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

您可以在这里阅读它的完整文档:https://nodejs.org/api/os.html

#4


4  

when you are using 32bits node on 64bits windows(like node-webkit or atom-shell developers), process.platform will echo win32

当您在64位窗口(如node-webkit或atom-shell开发人员)上使用32位节点时,请进行处理。平台将回声win32

use

使用

    function isOSWin64() {
      return process.arch === 'x64' || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');
    }

(check here for details)

(检查)

#5


1  

var isWin64 = process.env.hasOwnProperty('ProgramFiles(x86)');

#6


1  

const path = require('path');

if (path.sep === "\\") {
console.log("Windows");
} else {
console.log("Not Windows");
}

#7


1  

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.

i修饰符用于执行不区分大小写的匹配。

#8


0  

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")
}