0

Suppose I have a .env file like these:

#dev variable
PORT=3000
#production
PORT=3030

And I get these variables using process.env, how can I manage to sometimes use the dev variable and other times the Production variable

Yuri Aps
  • 929
  • 8
  • 13

2 Answers2

3

You can create multiple .env files like .dev.env, .prod.env, and load them based on NODE_ENV. using this

Akash Patel
  • 190
  • 1
  • 10
  • 1
    Thanks, here is better explained how to toogle btw the .env https://stackoverflow.com/questions/55406055/toggle-between-multiple-env-files-like-env-development-with-node-js – Yuri Aps Feb 27 '21 at 12:13
1

Storing configuration in environment variables is the way to go, and exactly what is recommended by the config in the 12-Factor App, so you're already starting with the right foot.

The values of these variables should not be stored with the code, except maybe the ones for your local development environment, which you can even assume as the default values:

port = process.env.PORT || '3000';

For all other environments, the values should be stored in a safe place like Vault or AWS Secrets Manager, and then are only handled by your deployment pipeline. Jenkins, for example, has a credentials plugin to handle that.

ericbn
  • 10,163
  • 3
  • 47
  • 55