In a recent nodejs project, I had a very strange occurrence: I set the HOSTNAME variable in my .env file, and by the time it got included in the .js file, the HOSTNAME value was different from the one in .env. Why?
--
.env
HOSTNAME=foo.bar.com
server.js
require('dotenv').config();
console.log("Hostname: ", process.env.HOSTNAME);
console output
Hostname: foo
Expected console output
Hostname: foo.bar.com
--
I have fixed the issue by renaming to _HOSTNAME, but I would like to know WHY this happened, and if there are other .env variable names that might be affected by this strange occurrence
dotenv
npm library skips the variables which already set in the host environment.You can find the more information about overriding existing system variables in official documentation.
https://github.com/motdotla/dotenv#what-happens-to-environment-variables-that-were-already-set