1 | # nconf
|
2 |
|
3 | [![Version npm](https://img.shields.io/npm/v/nconf.svg?style=flat-square)](https://www.npmjs.com/package/nconf)[![npm Downloads](https://img.shields.io/npm/dm/nconf.svg?style=flat-square)](https://www.npmjs.com/package/nconf)[![Build Status](https://img.shields.io/travis/indexzero/nconf/master.svg?style=flat-square)](https://travis-ci.org/indexzero/nconf)[![Dependencies](https://img.shields.io/david/indexzero/nconf.svg?style=flat-square)](https://david-dm.org/indexzero/nconf)
|
4 |
|
5 | [![NPM](https://nodei.co/npm/nconf.png?downloads=true&downloadRank=true)](https://nodei.co/npm/nconf/)
|
6 |
|
7 | Hierarchical node.js configuration with files, environment variables, command-line arguments, and atomic object merging.
|
8 |
|
9 | ## Example
|
10 | Using nconf is easy; it is designed to be a simple key-value store with support for both local and remote storage. Keys are namespaced and delimited by `:`. Let's dive right into sample usage:
|
11 |
|
12 | ``` js
|
13 | var fs = require('fs'),
|
14 | nconf = require('nconf');
|
15 |
|
16 | //
|
17 | // Setup nconf to use (in-order):
|
18 | // 1. Command-line arguments
|
19 | // 2. Environment variables
|
20 | // 3. A file located at 'path/to/config.json'
|
21 | //
|
22 | nconf.argv()
|
23 | .env()
|
24 | .file({ file: 'path/to/config.json' });
|
25 |
|
26 | //
|
27 | // Set a few variables on `nconf`.
|
28 | //
|
29 | nconf.set('database:host', '127.0.0.1');
|
30 | nconf.set('database:port', 5984);
|
31 |
|
32 | //
|
33 | // Get the entire database object from nconf. This will output
|
34 | // { host: '127.0.0.1', port: 5984 }
|
35 | //
|
36 | console.log('foo: ' + nconf.get('foo'));
|
37 | console.log('NODE_ENV: ' + nconf.get('NODE_ENV'));
|
38 | console.log('database: ' + nconf.get('database'));
|
39 |
|
40 | //
|
41 | // Save the configuration object to disk
|
42 | //
|
43 | nconf.save(function (err) {
|
44 | fs.readFile('path/to/your/config.json', function (err, data) {
|
45 | console.dir(JSON.parse(data.toString()))
|
46 | });
|
47 | });
|
48 | ```
|
49 |
|
50 | If you run the above script:
|
51 |
|
52 | ``` bash
|
53 | $ NODE_ENV=production sample.js --foo bar
|
54 | ```
|
55 |
|
56 | The output will be:
|
57 |
|
58 | ```
|
59 | foo: bar
|
60 | NODE_ENV: production
|
61 | database: { host: '127.0.0.1', port: 5984 }
|
62 | ```
|
63 |
|
64 | ## Hierarchical configuration
|
65 |
|
66 | Configuration management can get complicated very quickly for even trivial applications running in production. `nconf` addresses this problem by enabling you to setup a hierarchy for different sources of configuration with no defaults. **The order in which you attach these configuration sources determines their priority in the hierarchy.** Lets take a look at the options available to you
|
67 |
|
68 | 1. **nconf.argv(options)** Loads `process.argv` using yargs. If `options` is supplied it is passed along to yargs.
|
69 | 2. **nconf.env(options)** Loads `process.env` into the hierarchy.
|
70 | 3. **nconf.file(options)** Loads the configuration data at options.file into the hierarchy.
|
71 | 4. **nconf.defaults(options)** Loads the data in options.store into the hierarchy.
|
72 | 5. **nconf.overrides(options)** Loads the data in options.store into the hierarchy.
|
73 |
|
74 | A sane default for this could be:
|
75 |
|
76 | ``` js
|
77 | var nconf = require('nconf');
|
78 |
|
79 | //
|
80 | // 1. any overrides
|
81 | //
|
82 | nconf.overrides({
|
83 | 'always': 'be this value'
|
84 | });
|
85 |
|
86 | //
|
87 | // 2. `process.env`
|
88 | // 3. `process.argv`
|
89 | //
|
90 | nconf.env().argv();
|
91 |
|
92 | //
|
93 | // 4. Values in `config.json`
|
94 | //
|
95 | nconf.file('/path/to/config.json');
|
96 |
|
97 | //
|
98 | // Or with a custom name
|
99 | // Note: A custom key must be supplied for hierarchy to work if multiple files are used.
|
100 | //
|
101 | nconf.file('custom', '/path/to/config.json');
|
102 |
|
103 | //
|
104 | // Or searching from a base directory.
|
105 | // Note: `name` is optional.
|
106 | //
|
107 | nconf.file(name, {
|
108 | file: 'config.json',
|
109 | dir: 'search/from/here',
|
110 | search: true
|
111 | });
|
112 |
|
113 | //
|
114 | // 5. Any default values
|
115 | //
|
116 | nconf.defaults({
|
117 | 'if nothing else': 'use this value'
|
118 | });
|
119 | ```
|
120 |
|
121 | ## API Documentation
|
122 |
|
123 | The top-level of `nconf` is an instance of the `nconf.Provider` abstracts this all for you into a simple API.
|
124 |
|
125 | ### nconf.add(name, options)
|
126 | Adds a new store with the specified `name` and `options`. If `options.type` is not set, then `name` will be used instead:
|
127 |
|
128 | ``` js
|
129 | nconf.add('supplied', { type: 'literal', store: { 'some': 'config' });
|
130 | nconf.add('user', { type: 'file', file: '/path/to/userconf.json' });
|
131 | nconf.add('global', { type: 'file', file: '/path/to/globalconf.json' });
|
132 | ```
|
133 |
|
134 | ### nconf.use(name, options)
|
135 | Similar to `nconf.add`, except that it can replace an existing store if new options are provided
|
136 |
|
137 | ``` js
|
138 | //
|
139 | // Load a file store onto nconf with the specified settings
|
140 | //
|
141 | nconf.use('file', { file: '/path/to/some/config-file.json' });
|
142 |
|
143 | //
|
144 | // Replace the file store with new settings
|
145 | //
|
146 | nconf.use('file', { file: 'path/to/a-new/config-file.json' });
|
147 | ```
|
148 |
|
149 | ### nconf.remove(name)
|
150 | Removes the store with the specified `name.` The configuration stored at that level will no longer be used for lookup(s).
|
151 |
|
152 | ``` js
|
153 | nconf.remove('file');
|
154 | ```
|
155 |
|
156 | ## Storage Engines
|
157 |
|
158 | ### Memory
|
159 | A simple in-memory storage engine that stores a nested JSON representation of the configuration. To use this engine, just call `.use()` with the appropriate arguments. All calls to `.get()`, `.set()`, `.clear()`, `.reset()` methods are synchronous since we are only dealing with an in-memory object.
|
160 |
|
161 | ``` js
|
162 | nconf.use('memory');
|
163 | ```
|
164 |
|
165 | ### Argv
|
166 | Responsible for loading the values parsed from `process.argv` by `yargs` into the configuration hierarchy. See the [yargs option docs](https://github.com/bcoe/yargs#optionskey-opt) for more on the option format.
|
167 |
|
168 | ``` js
|
169 | //
|
170 | // Can optionally also be an object literal to pass to `yargs`.
|
171 | //
|
172 | nconf.argv({
|
173 | "x": {
|
174 | alias: 'example',
|
175 | describe: 'Example description for usage generation',
|
176 | demand: true,
|
177 | default: 'some-value'
|
178 | }
|
179 | });
|
180 | ```
|
181 |
|
182 | ### Env
|
183 | Responsible for loading the values parsed from `process.env` into the configuration hierarchy.
|
184 |
|
185 | ``` js
|
186 | //
|
187 | // Can optionally also be an Array of values to limit process.env to.
|
188 | //
|
189 | nconf.env(['only', 'load', 'these', 'values', 'from', 'process.env']);
|
190 |
|
191 | //
|
192 | // Can also specify a separator for nested keys (instead of the default ':')
|
193 | //
|
194 | nconf.env('__');
|
195 | // Get the value of the env variable 'database__host'
|
196 | var dbHost = nconf.get('database:host');
|
197 |
|
198 | //
|
199 | // Or use all options
|
200 | //
|
201 | nconf.env({
|
202 | separator: '__',
|
203 | match: /^whatever_matches_this_will_be_whitelisted/
|
204 | whitelist: ['database__host', 'only', 'load', 'these', 'values', 'if', 'whatever_doesnt_match_but_is_whitelisted_gets_loaded_too']
|
205 | });
|
206 | var dbHost = nconf.get('database:host');
|
207 | ```
|
208 |
|
209 | ### Literal
|
210 | Loads a given object literal into the configuration hierarchy. Both `nconf.defaults()` and `nconf.overrides()` use the Literal store.
|
211 |
|
212 | ``` js
|
213 | nconf.defaults({
|
214 | 'some': 'default value'
|
215 | });
|
216 | ```
|
217 |
|
218 | ### File
|
219 | Based on the Memory store, but provides additional methods `.save()` and `.load()` which allow you to read your configuration to and from file. As with the Memory store, all method calls are synchronous with the exception of `.save()` and `.load()` which take callback functions. It is important to note that setting keys in the File engine will not be persisted to disk until a call to `.save()` is made. Note a custom key must be supplied as the first parameter for hierarchy to work if multiple files are used.
|
220 |
|
221 | ``` js
|
222 | nconf.file('path/to/your/config.json');
|
223 | // add multiple files, hierarchically. notice the unique key for each file
|
224 | nconf.file('user', 'path/to/your/user.json');
|
225 | nconf.file('global', 'path/to/your/global.json');
|
226 | ```
|
227 |
|
228 | The file store is also extensible for multiple file formats, defaulting to `JSON`. To use a custom format, simply pass a format object to the `.use()` method. This object must have `.parse()` and `.stringify()` methods just like the native `JSON` object.
|
229 |
|
230 | If the file does not exist at the provided path, the store will simply be empty.
|
231 |
|
232 | ### Redis
|
233 | There is a separate Redis-based store available through [nconf-redis][0]. To install and use this store simply:
|
234 |
|
235 | ``` bash
|
236 | $ npm install nconf
|
237 | $ npm install nconf-redis
|
238 | ```
|
239 |
|
240 | Once installing both `nconf` and `nconf-redis`, you must require both modules to use the Redis store:
|
241 |
|
242 | ``` js
|
243 | var nconf = require('nconf');
|
244 |
|
245 | //
|
246 | // Requiring `nconf-redis` will extend the `nconf`
|
247 | // module.
|
248 | //
|
249 | require('nconf-redis');
|
250 |
|
251 | nconf.use('redis', { host: 'localhost', port: 6379, ttl: 60 * 60 * 1000 });
|
252 | ```
|
253 |
|
254 | ## Installation
|
255 |
|
256 | ### Installing npm (node package manager)
|
257 | ```
|
258 | curl http://npmjs.org/install.sh | sh
|
259 | ```
|
260 |
|
261 | ### Installing nconf
|
262 | ```
|
263 | [sudo] npm install nconf
|
264 | ```
|
265 |
|
266 | ## More Documentation
|
267 | There is more documentation available through docco. I haven't gotten around to making a gh-pages branch so in the meantime if you clone the repository you can view the docs:
|
268 |
|
269 | ```
|
270 | open docs/nconf.html
|
271 | ```
|
272 |
|
273 | ## Run Tests
|
274 | Tests are written in vows and give complete coverage of all APIs and storage engines.
|
275 |
|
276 | ``` bash
|
277 | $ npm test
|
278 | ```
|
279 |
|
280 | #### Author: [Charlie Robbins](http://nodejitsu.com)
|
281 | #### License: MIT
|
282 |
|
283 | [0]: http://github.com/indexzero/nconf-redis
|