.env vs config.json

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


This largely comes down to personal preference and the conventions of the frameworks you're using. They're just different formats for storing the same kind of information.

Some example config file formats:

  • .env
  • *.yml (YAML files)
  • *.ini (normally Windows-only)
  • *.json

At the end of the day, they all accomplish the same purpose: providing your application with environment-specific information (credentials, file paths, connection strings, etc.). Choose the format which best fits your choice of framework.


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.