A tiny module enabling nconf
to parse commented JSON files.
{
// User data
"user": "[email protected]", // an email address
"password": "Freedom",
// General setup
"environment": "production", // or "stage", "development"
...
}
Comments in JSON files can be helpful e.g. in configuration files. However, comments are not part of the JSON format and standard JSON tools such as JSON.parse
can't handle commented files. Instead, tools like jsonminify or strip-json-comments are required. This module enables the nconf module to parse commented JSON files using strip-json-comments.
Enable nconf
to parse commented JSON files by default:
var nconf = require('nconf');
require('nconf-strip-json-comments')(nconf);
// load file
nconf.file('path_to_some_commented_JSON_file');
Note that this will enable extended parsing globally, that is, for all occurrences of nconf
(also in nested modules).
You can also use the module's extended JSON format explicitly:
var nconf = require('nconf');
var commentedJsonFormat = require('nconf-strip-json-comments').format;
// load file
nconf.file({file: 'path_to_some_commented_JSON_file', format: commentedJsonFormat});
To disable a previously globally enabled parsing, use restore()
:
...
require('nconf-strip-json-comments').restore(nconf);
nconf-strip-json-comments is licensed under the BSD 3-Clause License.