
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
config-json
Advanced tools
A simple class for accessing JSON configuration files.
Use it to store all your app's configuration in replaceable JSON files. My personal use case is a system where multiple instances of the same app run with different configurations.
Note that require('config-json') will always return the same object. The idea
here is that you only have to load data in one place without having to handle
the same things elsewhere.
config-json will store data from multiple files simultaneously and lets you switch between them.
npm install config-json
(Use npm install config-json --save to automatically add to the dependencies
list in your package.json file.)
Basic example:
/*
Your example data.json file might look like this:
{
"key": {
"subkey": "An important value"
},
"key2": 500
}
*/
// Require the module, as usual.
var config = require('config-json');
// Load data from a JSON file.
config.load('./data.json');
// Retrieve data from that file
var myValue = config.get('key', 'subkey');
.get takes as many parameters as you want. You can drill down to any
`level of a JSON file that way.
var entire_file_contents = config.get();
var one_level_down = config.get('L1');
var two_levels_down = config.get('L1', 'L2');
You can also pass in an array of keys:
var the_code = ['UP', 'UP', 'DOWN', 'DOWN', 'LEFT', 'RIGHT', 'LEFT', 'RIGHT', 'B', 'A'];
var cheat = config.get(the_code);
You can load data from a JSON file, or you can pass in a key (we'll get to that in a moment) and an object of any sort yourself:
// Load data from a file
config.load('datafile');
// Load data from existing object
var myData = { ... };
config.load('important_things', myData);
Use .setBaseDir to pre-set the directory where your config files live. That way
you need only specify the file name for loading each file:
config.setBaseDir('./my_data_dir');
config.load('file1');
config.load('file2');
config.load('file3');
I mentioned a "key" above. When using .load multiple times, the data is stored
separately. When loading from files, the file argument you passed in is the key.
When loading data from an existing object, the key is manually set.
By default the last .load defines which object is currently being used. Using
.setKey you can switch between the data objects.
config.load('campbell');
config.load('algar');
var firstName = config.get('first_name'); // -> Garth
config.setKey('campbell');
firstName = config.get('first_name'); // -> Wayne
When using .get to fetch a key that does not exist, undefined is returned by
default. No error is raised. Note that this happens regardless of whether the
first or last key is missing.
By using .setDefault you can override the fallback return value.
var value = config.get('does', 'not', 'compute'); // -> undefined
config.setDefault("I'm sorry Dave, I'm afraid I can't do that");
var value = config.get('does', 'not', 'compute'); // -> "I'm sorry Dave, I'm afraid I can't do that"
Using clearCache() will delete all loaded data and invalidate Node's require cache. Useful for when the files have changed.
config.clearCache();
Using .createInstance() you can create a separate config instance that will not be coupled.
var Config = require('config-json');
var config_one = Config.createInstance.load('./data/file1.json');
var config_two = Config.createInstance.load('./data/file2.json');
Provide a separate interface object for each data file/object. Right now setKey will set the current key globally which may not be the desired effect.
FAQs
Access JSON based configuration files
The npm package config-json receives a total of 31 weekly downloads. As such, config-json popularity was classified as not popular.
We found that config-json demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.