Cross-platform hostname
On Linux and macOS, your hostname can be a fully-qualified domain name (e.g., my-linux-laptop.small-tech.org
). This can be used, for example, as an intelligent default for setting up a staging server and automatically provisioning TLS certificates.
On Windows 10, however, the situation is (of course, because Windows) different. Your hostname, as returned by os.hostname()
in Node is just your computer name (e.g., my-linux-laptop
).
To get the equivalent of a usable hostname as under Linux or macOS, you have to jump through a few hoops to set your primary DNS suffix, which is then combined with your hostname to give you your full computer name (or what we would call your hostname on Linux or macOS).
If you adhere to this convention on Windows 10, this module will return your full computer name (usable hostname) and you can write cross-platform code that relies on this.
How to set your full computer name on Windows 10
Say you want to set your hostname to my-windows-laptop.small-tech.org
:
- Control Panel → System And Security → System → Change Settings link (next to Computer name) → [Change…] Button
- Under Computer name, enter your subdomain (
my-windows-laptop
) - [More…] Button → enter your domain name (
small-tech.org
) in the Primary DNS suffix of this computer field. - Press the various [OK] buttons to dismiss the various modal dialogues and restart your computer.
Install
npm i @small-tech/cross-platform-hostname
Use
const hostname = require('@small-tech/cross-platform-hostname')
console.log(`Your hostname is ${hostname}`)
Copyright © 2019 Aral Balkan. License: AGPL version 3.0 or later.
Made with ❤️ by Small Technology Foundation. (Like this? Fund us.)