elanonrigby
elanonrigby

Reputation: 489

Reasoning between environment from express and dotenv

I've been seeing a lot of NodeJS and dotenv tutorials and articles where they define an ENV_MODE=development variable within the config.env file.

But require('express').get('env') already gives us the environment express is set on running. Why not just use the express env variable to check the environment mode? Why do we need to also set the explicit variable in the .env file?

Upvotes: 0

Views: 291

Answers (1)

Quentin
Quentin

Reputation: 944442

You have to set the environment variable somewhere.

Doing it in a .env file ties it to a specific computer, which means you don't have to either:

  • Remember to set it manually before you run the program
  • Bind it into package.json and risk running the wrong command when you run the code on staging/qa/production

… and it lets you keep it with any other environment variables you have (such as API keys) which shouldn't be committed to version control (since they are secrets).

Upvotes: 2

Related Questions