Yuri Aps
Yuri Aps

Reputation: 1099

Handle multiple environments variables in .env NodeJs

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

Upvotes: 0

Views: 4196

Answers (2)

ericbn
ericbn

Reputation: 10948

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.

Upvotes: 1

Akash Patel
Akash Patel

Reputation: 190

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

Upvotes: 3

Related Questions