简体   繁体   English

如何使用 Node.js 获取 Windows 版本?

[英]How to get Windows version using Node.js?

There are questions about OS version on the Stack Overflow but not about the Windows name, I am looking to find out Windows name using Node.js. Stack Overflow 上有关于操作系统版本的问题,但与 Windows 名称无关,我希望使用 Node.js 找出 Windows 名称。

I have looked into so many modules like os , platform , getos and using process etc. and found that these are helpful to get operating system description, process environment etc. I am able to get it is Linux or Windows too ie which platform I am using.我研究了很多模块,如osplatformgetos和 using process等,发现这些模块有助于获取操作系统描述、进程环境等。我也能得到它是 Linux 或 Windows,即我是哪个平台使用。

But, how can I check, is it Windows 7 or 8 which is installed on my system using Node.js?但是,我如何检查使用 Node.js 安装在我的系统上的 Windows 7 还是 Windows 8?

I am using kinect2 module in my Node.js project which is working fine on Windows 8 but I am looking to use it on Windows 7.我在 Node.js 项目中使用kinect2模块,该模块在 Windows 8 上运行良好,但我希望在 Windows 7 上使用它。

I have checked that Kinect2 will not work with Windows 7.我已经检查过 Kinect2 不适用于 Windows 7。

Use os.release() .使用os.release()

> os.release();
'10.0.18363'

On Windows, the result is in the form major.minor.build在 Windows 上,结果的格式为major.minor.build

Consult this table ( source ) to determine the version of Windows:请参阅此表( 来源)以确定 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

For Windows 10 specifically, consult this table ( source ) to determine the exact version:对于 Windows 10,请参阅此表( 来源)以确定确切版本:

 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

You can find the Windows version from the command line using ver .您可以使用ver从命令行查找 Windows 版本。 For example, on my machine:例如,在我的机器上:

>  ver

Microsoft Windows [Version 10.0.14393]

To execute this from node, use the child_process.execSync method:要从节点执行此操作,请使用child_process.execSync方法:

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

The whole .toString().trim() business is because the raw output from the command comes back as a Buffer , with newlines at the beginning and end.整个.toString().trim()业务是因为命令的原始输出作为Buffer返回,在开头和结尾带有换行符。

var os = require('os');
console.log(os.type());

refer to this link for more references: https://millermedeiros.github.io/mdoc/examples/node_api/doc/os.html有关更多参考,请参阅此链接: https : //millermedeiros.github.io/mdoc/examples/node_api/doc/os.html

The other alternative can be the npm library : "platform"另一种选择可以是 npm 库:“平台”

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM