Skip to content

.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?



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 "" --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.