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