'Despite requiring dotenv to load environment variables from .env file, they still get loaded from the machine /etc/environment

I am using dotenv to load environment variables from .env file when I am in the development environment.

server.js

const { config } = require("dotenv");
config();
console.log("🚀 ~ file: server.js ~ line 7 ~ config()", config());
console.log(
  "🚀 ~ file: server.js ~ line 10 ~ process.env.ADMIN_DB_URI",
  process.env.ADMIN_DB_URI
);

This is what gets logged:

🚀 ~ file: server.js ~ line 7 ~ config() {
  parsed: {
    ADMIN_DB_URI: 'URI_THAT_IS_SAVED_IN_DOTENV_FILE',
  }
}
🚀 ~ file: server.js ~ line 10 ~ process.env.ADMIN_DB_URI URI_THAT_IS_SAVED_IN_/ETC_/ENVIRONMENT_FILE 

So apparently loading the environment variable ADMIN_DB_URI from .env file works correctly as you can see in the log of the result of executing the config function.

But, when I log process.env.ADMIN_DB_URI right after that, it logs the value that is stored in /etc/environment.

This is confusing and I don't understand why it's happening.



Solution 1:[1]

After a few days I found out why this happens.
If you have an environment variable that exists both in .env file and in /etc/environment, nodejs will load the one /etc/environment.

So all I had to do is rename the variable in .env.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AG_HIHI