How to get the version number of the OS in Node.js?

Published March 23, 2021

To get the version number of the OS, you can use the release() method from the os module in Node.js.

/* Get version number of os in Node.js */

// import os module
const os = require("os");

// get version number of os
const versionNumber = os.release(); // 20.3.0
  • The method returns the version number of the OS currently installed as a string.

See the above code live in repl.it.

You can also use the platform() method from the os module to get the operating system name along with the version number.

It can be done like this,

const os = require("os");

// get name and version number of os
const versionNumber = os.release();
const operatingSystemName = os.platform();

console.log(versionNumber, operatingSystemName); // darwin 20.3.0

That's all! 😃

Feel free to share if you found this useful 😃.


Share on: Facebook Twitter