Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.env vs config.json

I just started learning JavaScript / node.js (a gentle introduction to back-end webdev), and thus I am completely green in the subject.

A few days ago I was reading a tutorial from which I learned to keep my confidential data (like passwords) in a config.json file.

Today I have discovered (by a chance) .env file, and the more I learn about it, it seems, the more people are using it to actually store passwords.

So when should I use .env and when should I use config.json?

like image 544
PatrykB Avatar asked Aug 20 '18 19:08

PatrykB


People also ask

Is .env a config file?

In case you are still wondering what all this means, well, you are probably new to the . env file. It's actually a simple configuration text file that is used to define some variables you want to pass into your application's environment. This file needs a something like a parser to make it work.

What is an env json file?

The env. json file is a project-specific list of accessible variables. This file is the ideal place to store secret keys, project-wide properties, or anything else you want to obfuscate or share between your files. It is important to note that env. json is limited to a flat key/value pair system.

When should I use an .env file?

This. env. example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.

Should you use .env in production?

Using environment variables is a somewhat common practice during Development but it is actually not a healthy practice to use with Production. While there are several reasons for this, one of the main reasons is that using environment variables can cause unexpected persistence of variable values.


2 Answers

The Question

When should .env be used over config.json and for what?

The answer

This is a rather difficult answer. On the one hand, you should only really ever use either of these tools while in development. This means that when in a production or prod-like environment, you would add these variables directly to the environment:

NODE_ENV=development node ./sample.js --mongodb:host "dharma.mongohq.com" --mongodb:port 10065 

There is no real clear winner over the other per se as they are both helpful in different ways. You can have nested data with config.json, but on the other hand, you can also have a cleaner data structure with .env

Some thing also to note is that you never want to commit these files to source control (git, svc etc).

On the other hand, these tools make it very easy for beginners to get started quickly without having to worry about how to set the environment variables and the differences between a windows environment and a linux one.

All in all, I'd say its really up to the developer.

like image 174
Derek Pollard Avatar answered Sep 30 '22 05:09

Derek Pollard


.env files are generally used to store information related to the particular deployment environment, while config.json files might be used to store data particular to the application as a whole.

either approach works, and whether or not your config files are stored in your repository is more a function of whether the data needs to be confidential.

like image 44
derelict Avatar answered Sep 30 '22 05:09

derelict