Configuration Files

Formats

.env

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
1
NODE_ENV=production
2
PORT=3001
Copied!

JSON or JSON5

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
1
{
2
// comments
3
unquoted: 'and you can quote me on that',
4
singleQuotes: 'I can use "double quotes" here',
5
lineBreaks: "Look, Mom! \
6
No \\n's!",
7
hexadecimal: 0xdecaf,
8
leadingDecimalPoint: .8675309, andTrailing: 8675309.,
9
positiveSign: +1,
10
trailingComma: 'in objects', andIn: ['arrays',],
11
"backwardsCompatible": "with JSON",
12
}
Copied!

XML

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

YAML (YAML Ain't Markup Language or Yet Another Markup Language)

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

INI

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

Should you mix formats?

Reasons to mix: Yes

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.

Reasons not to mix: No

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)

Should you create your own?

No. Unless you have a really good reason.
You have to create a parser.