A simple, dependency-free configuration helper with a nano API.
var config = require('nano-config')('./my-configuration.json');
config.greet = 'world';
config.save()
Loads my-configuration.json
. If it doesn't exist it will be created when you call save
.
{
"greet": "world"
}
Okay, cool. You just saved some JSON. Who cares?
You only need to load a configuration file once. Here's some other part of your application.
var config = require('nano-config');
console.log(`hello ${config.greet}`);
Outputs
hello world
nano-config remembers the last config you loaded.
nano-config doesn't depend on any modules, doesn't care about environment variables and doesn't have a verbose API.
Do you need to load a specific configuration file for a specific environment? Do it yourself.
var config = require('nano-config')(`./${process.env.NODE_ENV}.json`)
Custom serialization. Prefer CSON?
var CSON = require('cson');
var config = require('nano-config');
config.deserialize = str => CSON.parseSync(str);
config.serialize = obj => CSON.stringifySync(obj);
config('./my-config.cson');
Loads a configuration file.
Saves the current configuration to file. Optional callback is called when save is finished.
The object serializer. It takes an object and returns a string.
The string deserializer. It takes a string and returns an object.