Node.js - hostname

by
Jeremy Canfield |
Updated: January 21 2025
| Node.js articles
os can be used to get the system hostname in Node.js. In this example, the full system hostname will be printed.
const os = require("os");
const hostname = os.hostname();
console.log(`hostname = ${hostname}`);
Be aware that if you are using a Node.js module instead of CommonJS, you will have "type": "module" in package.json. In this scenario, you will use import instead of require.
from os import "os";
const hostname = os.hostname();
console.log(`hostname = ${hostname}`);
Did you find this article helpful?
If so, consider buying me a coffee over at