Stack Overflow 上有关于操作系统版本的问题,但没有关于 Windows 名称的问题,我希望使用 Node.js 找出 Windows 名称。
我研究了很多模块,如
os
,platform
,getos
和使用process
等,发现这些有助于获取操作系统描述,进程环境等。我能够得到它是Linux 或 Windows 也是如此,即我正在使用哪个平台。
但是,我如何检查我的系统上使用 Node.js 安装的是 Windows 7 还是 8?
我在 Node.js 项目中使用
kinect2
模块,该模块在 Windows 8 上运行良好,但我希望在 Windows 7 上使用它。
我已检查 Kinect2 不适用于 Windows 7。
os.release()
。
> os.release();
'10.0.18363'
在 Windows 上,结果的形式为
major.minor.build
查阅此表(来源)以确定 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,请查阅此表(来源)以确定确切的版本:
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
您可以使用
ver
从命令行找到 Windows 版本。例如,在我的机器上:
> ver
Microsoft Windows [Version 10.0.14393]
要从节点执行此操作,请使用
child_process.execSync
方法:
var versionString = require('child_process').execSync('ver').toString().trim()
整个
.toString().trim()
业务是因为命令的原始输出作为 Buffer
返回,在开头和结尾都有换行符。
仅供参考,
os.release()
在win11中也会返回10.x.x,这仍然没有解决
获取您的操作系统类型:
let arch = process.platform;
您可以找到您的操作系统版本号(如上所述):
let os = require('node:os');
let release = os.release();
var os = require('os');
console.log(os.type());
请参阅此链接以获取更多参考: https://millermedeiros.github.io/mdoc/examples/node_api/doc/os.html
另一个替代方案可以是 npm 库:“平台”