• Node.js Video Tutorials

Node.js - os.release() 方法



Node.js os.release() 方法返回一个字符串值,该值指定操作系统版本。对于 UNIX 和 LINUX 系统,该函数使用名为 uname 的命令识别操作系统。在 Windows 上,使用 Win32 API 中名为 GetVersionExW() 的函数。

语法

以下是 Node.js os.release() 方法的语法:

os.release()

参数

此方法不接受任何参数。

返回值

此方法返回一个字符串,指示操作系统的版本。

示例

在以下示例中,我们尝试在Windows 操作系统中执行Node.js os.release() 方法

const os = require('os');
console.log('On windows');
console.log(os.release());

输出

3.10.0-1160.76.1.el7.x86_64

注意 - 要获得准确的结果,最好在本地执行上述代码。

如果我们编译并运行上述程序,则os.release() 方法将打印当前操作系统的版本。

10.0.22621

示例

在以下示例中,我们尝试在 LINUX 操作系统中执行os.release() 方法

const os = require('os');
console.log('On LINUX');
console.log(os.release());

输出

在执行上述程序后,os.release() 方法将返回当前操作系统的版本。

5.16.0-kali7-amd64

示例

在本例中,我们打印当前操作系统的平台和版本。

const os = require('os');
var platform = os.platform();
var release = os.release();
if(platform === 'win32'){
   console.log("The platform: " + platform);
   console.log("The version: " + release);
}

输出

注意 - 要获得准确的结果,最好在本地执行上述代码。

执行上述程序后,os.platform() 方法打印操作系统平台,os.release() 方法打印操作系统版本。

The platform: win32
The version: 10.0.22621
nodejs_os_module.htm
广告

© . All rights reserved.