Module.exports vs plain json for config files

config, json, node.js

Solution

javascript CommonJS Module

- comments

- conditionals

- loops and such to populate defaults

- code to change config based on NODE_ENV or similar

- code to look for external files for SSL keys, API credentials, etc

- easier to have fallbacks and defaults

JSON file

- easy to parse and update with external tools

- compatible with pretty much every programming language out there

- pure data that can be loaded without being executed

- easy to pretty print

- JSON could start as the basis and all the code items described above about CommonJS module could live in a config.js module that reads config.json as it's starting point

So I always start with a commonjs module for the convenience, but keep any logic in there simple. If your config.js has bugs and needs tests, it's probably too complicated. KISS. If I know for a fact other things are going to want poke around in my config, I'll use a JSON file.

Problem

I see multiple ways to create config files in Node.js. One uses module.exports in js file, one just use plain json object. ``` // config1.js module.exports = { config_1: "value 1", config_2: "value 2" } ``` ``` // config2.json { "config_1": "value 1", "config_2": "value 2" } ``` Is there any advantages of using module.exports in config file? What are the differences?

Original source