【问题标题】:How to get Windows version using Node.js?如何使用 Node.js 获取 Windows 版本?
【发布时间】:2017-07-20 08:38:18
【问题描述】:

Stack Overflow 上有关于操作系统版本的问题,但不是关于 Windows 名称的问题,我希望使用 Node.js 找出 Windows 名称。

我研究了很多模块,如osplatformgetos 和使用process 等,发现这些有助于获取操作系统描述、进程环境等。我能够得到它也是 Linux 或 Windows,即我使用的是哪个平台。

但是,我如何检查是使用 Node.js 安装在我的系统上的 Windows 7 还是 8?

我在我的 Node.js 项目中使用 kinect2 模块,该模块在 Windows 8 上运行良好,但我希望在 Windows 7 上使用它。

我已经检查过 Kinect2 不能在 Windows 7 上运行。

【问题讨论】:

标签: javascript node.js kinect node-modules kinect-v2


【解决方案1】:
var os = require('os');
console.log(os.type());

请参阅此链接以获取更多参考: https://millermedeiros.github.io/mdoc/examples/node_api/doc/os.html

另一种选择可以是 npm 库:“平台”

看看这个:https://www.npmjs.com/package/platform

【讨论】:

  • 我认为几乎所有版本的 Windows 都会返回 Windows_NT,对吧?
  • 是的,我正在获取 windows_NT。实际上是什么意思?
  • 好吧,我没有在 Windows 操作系统上尝试过。但我认为另一种选择可能是使用库:“平台”。看看这个:npmjs.com/package/platform
  • @Krisalay 感谢您的回答,我从 rossipedia 的回答中得到了帮助。
  • @Krisalay 平台模块也没有帮助。
【解决方案2】:

您可以使用ver 从命令行查找 Windows 版本。例如,在我的机器上:

>  ver

Microsoft Windows [Version 10.0.14393]

要从节点执行此操作,请使用child_process.execSync 方法:

var versionString = require('child_process').execSync('ver').toString().trim()

整个.toString().trim() 业务是因为命令的原始输出返回为Buffer,开头和结尾都有换行符。

【讨论】:

  • 我已经编辑了答案。使用这个我可以获得窗口名称,但我不是在寻找 Jugaad(不知道用英语叫什么 jugaad ^_^)。我正在寻找标准代码。
  • 据我所知,没有特定于节点的 API 会返回运行它的操作系统的特定版本。您将不得不求助于调用特定于操作系统的 API(例如 Windows 上的 ver 和 Linux 上的 uname)来获取有关操作系统的详细信息
  • @DeepKakkar 在编程方面,“jugaad”可以粗略地翻译为 hack
  • 在 Windows 7 的 git bash 中返回 MINGW64_NT-6.1
【解决方案3】:

使用os.release()

> os.release();
'10.0.18363'

在 Windows 上,结果格式为 major.minor.build

请查阅此表 (source) 以确定 Windows 的版本:

 Version                                    major.minor   
------------------------------------------ ------------- 
 Windows 10, Windows Server 2016            10.0
 Windows 8.1, Windows Server 2012 R2        6.3
 Windows 8, Windows Server 2012             6.2
 Windows 7, Windows Server 2008 R2          6.1
 Windows Vista, Windows Server 2008         6.0
 Windows XP Professional x64 Edition,       5.2
 Windows Server 2003, Windows Home Server
 Windows XP                                 5.1
 Windows 2000                               5.0

对于 Windows 10,请参阅此表 (source) 以确定确切的版本:

 Version           build
----------------- -------
 Windows 10 1909   18363
 Windows 10 1903   18362
 Windows 10 1809   17763
 Windows 10 1803   17134
 Windows 10 1709   16299
 Windows 10 1703   15063
 Windows 10 1607   14393
 Windows 10 1511   10586
 Windows 10 1507   10240

【讨论】:

  • 您好,如何在不使用内部版本号的情况下检测平台是 Windows Server 2016 还是 Windows 10?
  • @Eghes 我不确定 Node.js API 是否可以做到这一点。您可能必须运行 Windows 命令来获取 Windows 版本,使用 child_process.exec(),例如 wmic os get caption,并解析输出。
猜你喜欢
  • 2017-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-29
  • 2023-04-05
  • 1970-01-01
  • 2020-12-19
相关资源
最近更新 更多