Node.js Reference ยท path, os and url
Node.js os.hostname() method
Reference for os.hostname() and os.machine(), os.version() and os.release() in Node.js: identify the host a program runs on and use it safely in logs.
os.hostname() returns the name of the machine as a string, the same value the hostname command prints. It is commonly attached to log lines, metrics and error reports so you can tell which server produced them.
Closely related functions describe the system itself: os.release() gives the kernel or OS release, os.version() a longer version string and os.machine() the hardware architecture such as x86_64 or arm64.
import os from "node:os"; console.log(os.hostname());
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
os.hostname(): string os.machine(): string os.release(): string os.version(): string
| Name | Description |
|---|---|
| (none) | None of these functions takes arguments. |
Returns: a string. machine() returns values such as x86_64, arm64 or aarch64 (the exact spelling depends on the platform). release() is for example a kernel version on Linux.
Edge cases: the hostname can be any text the administrator chose, including mixed case or a fully qualified name. In containers it is usually the container ID unless set explicitly.
Usage notes
- A hostname is not an identity. Two machines can share a name and a machine can change its name, so use a generated instance ID where uniqueness matters.
- Do not trust it for security decisions. It is configuration, not proof of anything.
- Cache it if you log in a hot path. The value rarely changes while a process runs.
- Windows: the name is the computer name; on all platforms the value is read at call time.
os.machine()was added in Node.js 18.9 and 16.18. It describes the hardware, whileprocess.archdescribes the architecture Node.js itself was built for.