How to get the hostname of the OS in Node.js?

March 21, 2021 - 1 min read

To get the name or hostname of the OS, you can use the hostname() method from the os module in Node.js.

/* Get hostname of os in Node.js */

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

// get host name
const hostName = os.hostname();
  • The method returns the name of the system where the OS currently is installed as a string.

See the above code live in repl.it.

Feel free to share if you found this useful 😃.