You must accept both.
JavaScript configuration files have many advantages:
- programmable, of course, which takes into account interesting extensions for the generated parts
- lighter (e.g. fewer quotes)
- allows you to have comments (a big JSON limitation, which is why they are needed )
- other types of values ββ(
NaN and infinite, for example, regular expressions)
In this case, the JS file exports a simple JS object, similar to the one you would have from parsing the JSON file.
JSON is more socially acceptable because JS is often not considered as a configuration format. Allowing people to use JSON will prevent the "JS is for logic" fruitless debate, and there will be no problem if people are happy with it.
We accept both formats:
Here's how to read the JS / JSON configuration:
try { config = require('./config.js'); } catch(err) { if (err.code==='MODULE_NOT_FOUND') { config = require('./config.json'); } else { console.error('config.js loading failed'); throw err; } }
In any case, the JSON configuration file can be turned into a JS file by simply adding the prefix to it
module.exports =
so no one is locked in this format.
source share