Configuration Files

Properties:
  • Simple configuration
  • Values can only be a string or a number
  • No complicated values
  • Useful when making minor flags or changes
  • Human readable
Example:
.env
NODE_ENV=production
PORT=3001

If possible use JSON5 over JSON.
The JSON5 Data Interchange Format (JSON5) is a superset of JSON that aims to alleviate some of the limitations of JSON by expanding its syntax to include some productions from ECMAScript 5.1.
Properties:
  • Nested configuration
  • Uses an already familiar programming paradigm
  • Human readable
Example:
example.json5
{
// comments
unquoted: 'and you can quote me on that',
singleQuotes: 'I can use "double quotes" here',
lineBreaks: "Look, Mom! \
No \\n's!",
hexadecimal: 0xdecaf,
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
positiveSign: +1,
trailingComma: 'in objects', andIn: ['arrays',],
"backwardsCompatible": "with JSON",
}

Properties:
  • Complicated
  • Overly verbose
  • Useful for computers interfacing with each other

Properties:
  • Can get complex with the "repeated nodes" syntax
  • Spacing is important, which can throw off the configuration
  • Isn't immediately obvious when something is a list/array or a map

Properties:
  • Cannot be deeply nested
; last modified 1 April 2001 by John Doe
[owner]
name=John Doe
organization=Acme Widgets Inc.
[database]
; use IP address in case network name resolution is not working
server=192.0.2.62
port=143
file="payroll.dat"

Example: mixing .env and JSON5
When you need a mix of simple and complex data structures?
JSON objects should only be read once.
Environment variables can be read multiple times in the system.
Limit it to just two.

Don't mix too many formats. Too many different rules to remember. Or use different format for communication between services (ie: JSON, or XML files)

No. Unless you have a really good reason.
You have to create a parser.
Copy link
On this page
Formats
.env
JSON or JSON5
XML
YAML (YAML Ain't Markup Language or Yet Another Markup Language)
INI
Should you mix formats?
Reasons to mix: Yes
Reasons not to mix: No
Should you create your own?