subrunner
subrunner

Reputation: 404

process.env variables not the same value as in .env config

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?

--

Situation

.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

--

Debugging

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

Upvotes: 2

Views: 2257

Answers (1)

prabhakaran
prabhakaran

Reputation: 333

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

Upvotes: 4

Related Questions