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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8683895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:57:05  来源:igfitidea点击:

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

node.js

提问by Mauvis Ledford

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?

回答by Mauvis Ledford

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 上,变量返回darwin. 在 Windows 上,它会返回win32(即使在 64 位上)。

Current possible valuesare:

当前可能的值是:

  • aix
  • darwin
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32
  • aix
  • darwin
  • freebsd
  • linux
  • openbsd
  • sunos
  • win32

I just set this at the top of my jakeFile:

我只是将其设置在 jakeFile 的顶部:

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

回答by Benny Neugebauer

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

Node.js v6(及更高版本)有一个专用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#os_os_type

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

回答by alessioalex

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.

操作系统平台()

返回操作系统平台。可能的值为 'darwin'、'freebsd'、'linux'、'sunos' 或 'win32'。返回 process.platform 的值。

回答by Jaidee

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());

回答by Adeojo Emmanuel IMM

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

回答by Vlad

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 修饰符用于执行不区分大小写的匹配。

回答by user2404131

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

当您在 64 位 Windows 上使用 32 位节点时(如 node-webkit 或 atom-shell 开发人员),process.platform 将回显 win32

use

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

(check herefor details)

(详情请点击此处

回答by Alexander Koleda

const path = require('path');

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

回答by Ziyu Zhou

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

回答by Shagun Pruthi

I was facing the same issue running my node js code on Windows VM on mac machine. The following code did the trick.

我在 mac 机器上的 Windows VM 上运行我的节点 js 代码时遇到了同样的问题。下面的代码成功了。

Replace

代替

process.platform == 'win32'

process.platform == 'win32'

with

const os = require('os');

os.platform() == 'win32';

const os = require('os');

os.platform() == 'win32';