DEV Community

Cover image for How to get the version number of the OS in Node.js?
MELVIN GEORGE
MELVIN GEORGE

Posted on • Originally published at melvingeorge.me

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

Originally posted here!

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

That's all! 😃

Feel free to share if you found this useful 😃.


Oldest comments (0)