• Node.js Video Tutorials

Node.js - os.version() Method



The os module of Node.js provides a bunch of operating system-related utility methods and properties.

The Node.js os.version() method will return a string that represents the kernel version of the current operating system. Whereas on POSIX systems, the operating system release is specified by calling with a command named uname. On Windows, if the RtlGetVersion() is not available, GetVersionExW() will be used.

Syntax

Following is the syntax of the Node.js os.version() method −

os.version()

Parameters

This method does not accept any parameters.

Return value

This method returns a string that specifies the kernel version of the current operating system.

Example

In the following example, we are trying to print the kernel version of the current operating system by using the Node.js os.version() method.

const os = require('os');

console.log(os.version());

Error

/home/cg/root/63a002c52763b/main.js:3
console.log(os.version());
   ^

TypeError: os.version is not a function
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:16)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − To get the accurate result, better execute the above code in local.

After executing the above program, the os.verison() method returns the kernel version of the current operating system.

Windows 10 Pro

Example

In this example, we are trying to implement another way to print the kernel version of the current operating system.

const os = require('os');
var version = os.version();
var curr_sys_ver = 'Windows 10 Pro';
if (version == curr_sys_ver){
   console.log('The Version of current system is: ' + version);
}

Output

home/cg/root/63a002c52763b/main.js:3
var version = os.version();
   ^

TypeError: os.version is not a function
   at Object.<anonymous> (/home/cg/root/63a002c52763b/main.js:3:18)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Note − To get the accurate result, better execute the above code in local.

If we compile and run the above program, the os.verison() method returns the kernel version of the current system.

The Version of current system is: Windows 10 Pro
nodejs_os_module.htm
Advertisements