|
1 | 1 | # nodejs-read-config
|
2 | 2 |
|
3 |
| -[](https://travis-ci.org/coditorium/nodejs-read-config) |
4 |
| -[](https://david-dm.org/coditorium/nodejs-read-config) |
5 |
| -[](https://coveralls.io/r/coditorium/nodejs-read-config) |
| 3 | +The original author, @coditorium, has not responded to issues for years and appears to have abandoned this project. |
6 | 4 |
|
7 |
| -[](https://www.npmjs.com/package/read-config) |
| 5 | +I have taken it over as "read-config-ng" and the new repo can be found here: |
8 | 6 |
|
9 |
| -Multi format configuration loader for Node.js. |
10 |
| -Features: |
| 7 | + [read-config-ng](https://github.com/billchurch/read-config-ng) |
11 | 8 |
|
12 |
| -- Environmental variables replacement |
13 |
| -- Configuration variables replacement |
14 |
| -- Overriding configuration properties via environmental variables |
15 |
| -- Variable default values |
16 |
| -- Hierarchical configurations |
17 |
| -- Supported format: |
18 |
| - - [JSON5](http://json5.org/) |
19 |
| - - [YAML](http://en.wikipedia.org/wiki/YAML) |
20 |
| - - [Properties](http://en.wikipedia.org/wiki/.properties) |
| 9 | +This is also installable through npm as `read-config-ng` |
21 | 10 |
|
22 |
| -## How to use |
23 |
| - |
24 |
| -### Environment variable replacement |
25 |
| - |
26 |
| -/tmp/config.json: |
27 |
| -``` javascript |
28 |
| -{ env1: "%{ENV_VAR1}", env2: "%{ENV_VAR2|def}" } |
29 |
| -``` |
30 |
| -index.js: |
31 |
| -``` javascript |
32 |
| -var readConfig = require('read-config'), |
33 |
| - config = readConfig('/tmp/config.json'); |
34 |
| - |
35 |
| -console.log(config); |
36 |
| - |
37 |
| -// $ ENV_VAR1=abc node index.js |
38 |
| -// { env1: 'abc', env2: 'def' } |
39 |
| -``` |
40 |
| - |
41 |
| -- It is possible to change `%` to any other character. Just use `replaceEnv` configuration option. |
42 |
| -- It is possible to use default values when environmental variable is not set. |
43 |
| - |
44 |
| -### Configuration overriding with system variables |
45 |
| - |
46 |
| -/tmp/config.json: |
47 |
| -``` javascript |
48 |
| -{ |
49 |
| - rootProp: "rootProp", |
50 |
| - objProp: { |
51 |
| - x: 'X' |
52 |
| - } |
53 |
| -} |
54 |
| -``` |
55 |
| -index.js: |
56 |
| -``` javascript |
57 |
| -var readConfig = require('read-config'), |
58 |
| - config = readConfig('/tmp/config.json', { override: true }); |
59 |
| - |
60 |
| -console.log(config); |
61 |
| - |
62 |
| -// $ ENV_VAR1=abc node index.js |
63 |
| -// { rootProp: 'rootProp', objProp: { x: 'X'} } |
64 |
| - |
65 |
| -// $ CONFIG_objProp_x=abc node index.js |
66 |
| -// { rootProp: 'rootProp', objProp: { x: 'abc'} } |
67 |
| -``` |
68 |
| - |
69 |
| -- It is possible to change `CONFIG` to any other character. Just use `override` configuration option. |
70 |
| -- It is possible to override existing value or create new one. |
71 |
| - |
72 |
| -### Configuration variable replacement |
73 |
| - |
74 |
| -/tmp/config.json: |
75 |
| -``` javascript |
76 |
| -{ |
77 |
| - text1: "def", |
78 |
| - text2: "abc-@{text1}-ghi" |
79 |
| - number1: 1, |
80 |
| - number2: "@{number1}", |
81 |
| - boolean1: true, |
82 |
| - boolean2: "@{boolean1}", |
83 |
| - null1: null, |
84 |
| - null2: "@{null1}", |
85 |
| - obj1: { |
86 |
| - x: 'X', |
87 |
| - y: '@{./x}', // same as @{obj1.x} |
88 |
| - z: '@{../text1}' // same as @{text1} |
89 |
| - }, |
90 |
| - obj2: "@{obj1}" |
91 |
| -} |
92 |
| -``` |
93 |
| -index.js: |
94 |
| -``` javascript |
95 |
| -var readConfig = require('read-config'), |
96 |
| - config = readConfig('/tmp/config.json'); |
97 |
| - |
98 |
| -console.log(config); |
99 |
| - |
100 |
| -// $ node index.js |
101 |
| -// { |
102 |
| -// text1: "def", |
103 |
| -// text2: "abc-def-ghi" |
104 |
| -// number1: 1, |
105 |
| -// number2: 1, |
106 |
| -// boolean1: true, |
107 |
| -// boolean2: true, |
108 |
| -// null1: null, |
109 |
| -// null2: null, |
110 |
| -// obj1: { |
111 |
| -// x: 'X', |
112 |
| -// y: 'X', |
113 |
| -// z: 'def' |
114 |
| -// }, |
115 |
| -// obj2: { |
116 |
| -// x: 'X', |
117 |
| -// y: 'X', |
118 |
| -// z: 'def' |
119 |
| -// } |
120 |
| -// } |
121 |
| -``` |
122 |
| - |
123 |
| -- It is possible to use nested paths like `@{x.y.z}` |
124 |
| -- It is possible to use relative paths like `@{./x}` and `@{../y}` |
125 |
| -- It is possible to concatenate variables like `@{x}abc@{y}def@{ghi}` |
126 |
| - |
127 |
| -### Configuration hierarchy |
128 |
| - |
129 |
| -/tmp/config-1.json: |
130 |
| -``` javascript |
131 |
| -{ |
132 |
| - a: "a", |
133 |
| - b: "b", |
134 |
| - arr: [1, 2, 3] |
135 |
| -} |
136 |
| -``` |
137 |
| -/tmp/config-2.json: |
138 |
| -``` javascript |
139 |
| -{ |
140 |
| - __parent: "/tmp/config-1.json", |
141 |
| - // same as: __parent: "./config-1.json", |
142 |
| - b: "bb", |
143 |
| - c: "aa", |
144 |
| - arr: [] |
145 |
| -} |
146 |
| -``` |
147 |
| -index.js: |
148 |
| -``` javascript |
149 |
| -var readConfig = require('read-config'), |
150 |
| - config = readConfig('/tmp/config-2.json'); |
151 |
| - |
152 |
| -console.log(config); |
153 |
| - |
154 |
| -// $ node index.js |
155 |
| -// { |
156 |
| -// a: "a" |
157 |
| -// b: "bb", |
158 |
| -// c: "aa", |
159 |
| -// arr: [] |
160 |
| -// } |
161 |
| - |
162 |
| -``` |
163 |
| - |
164 |
| -### Hierarchy and basedir |
165 |
| - |
166 |
| -/tmp/config-1.json: |
167 |
| -``` javascript |
168 |
| -{ |
169 |
| - a: "a", |
170 |
| - b: "b", |
171 |
| - arr: [1, 2, 3] |
172 |
| -} |
173 |
| -``` |
174 |
| -/home/xxx/config-2.json: |
175 |
| -``` javascript |
176 |
| -{ |
177 |
| - __parent: "config-1", // no directory & extension specified |
178 |
| - b: "bb", |
179 |
| - c: "aa", |
180 |
| - arr: [] |
181 |
| -} |
182 |
| -``` |
183 |
| -index.js: |
184 |
| -``` javascript |
185 |
| -var readConfig = require('read-config'), |
186 |
| - config = readConfig('/tmp/config-2.json'); |
187 |
| - |
188 |
| -console.log(config); |
189 |
| - |
190 |
| -// $ node index.js |
191 |
| -// { |
192 |
| -// a: "a" |
193 |
| -// b: "bb", |
194 |
| -// c: "aa", |
195 |
| -// arr: [] |
196 |
| -// } |
197 |
| -``` |
198 |
| - |
199 |
| -### YAML config format |
200 |
| - |
201 |
| -Using YAML representation lookout for special characters like: '%' and '@'. |
202 |
| - |
203 |
| -/tmp/config.yml: |
204 |
| -``` javascript |
205 |
| -a: "@{LOCAL_VAR}" |
206 |
| -b: "%{ENV_VAR}" |
207 |
| -c: No quotes needed! |
208 |
| -``` |
209 |
| - |
210 |
| -## API |
211 |
| - |
212 |
| -### Functions |
213 |
| - |
214 |
| -- **readConfig(paths, [opts])** - Alias for `readConfig.sync(paths, [opts])`. |
215 |
| -- **readConfig.sync(paths, [opts])** - Loads configuration file synchronously. |
216 |
| -- **readConfig.async(paths, [opts], callback)** - Loads configuration file asynchronously. |
217 |
| - |
218 |
| -All json files are loaded using [JSON5](https://www.npmjs.com/package/json5) library. It means you can add comments, and skip quotes in your config files - thank you json5;). |
219 |
| - |
220 |
| -### Parameters |
221 |
| - |
222 |
| -- **paths** (String/Array) - path (or array of paths) to configuration file. If passed an array of paths than every configuration is resolved separately than merged hierarchically (like: [grand-parent-config, parent-config, child-config]). |
223 |
| -- **opts** (Object, optional) - configuration loading options |
224 |
| - - **parentField** - (Boolean/String, default: true) if specified enables configuration hierarchy. It's value is used to resolve parent configuration file. This field will be removed from the result. A string value overrides `__parentField` property name. |
225 |
| - - **optional** - (String/Array, default: []) list of configuration paths that are optional. If any configuration path is not resolved and is not optional it's treated as empty file and no exception is raised. |
226 |
| - - **basedir** - (String/Array, default: []) base directory (or directories) used for searching configuration files. Mind that `basedir` has lower priority than a configuration directory, process basedir, and absolute paths. |
227 |
| - - **replaceEnv** - (Boolean/String, default: false, constraint: A string value must be different than `replaceLocal`) if specified enables environment variable replacement. Expected string value e.g. `%` that will be used to replace all occurrences of `%{...}` with environment variables. You can use default values like: %{a.b.c|some-default-value}. |
228 |
| - - **replaceLocal** - (Boolean/String, default: '@', constraint: A string value must be different than `replaceEnv`) if specified enables configuration variable replacement. Expected string value e.g. `@` that will be used to replace all occurrences of `@{...}` with configuration variables. You can use default values like: @{a.b.c|some-default-value}. |
229 |
| - - **override** - (Boolean/String, default: false) If specified enables configuration overriding with environmental variables like `CONFIG_<propertyName>`. |
230 |
| - - **skipUnresolved** - (Boolean, default: false) `true` blocks error throwing on unresolved variables. |
231 |
| - |
232 |
| -Default **opts** values: |
233 |
| -``` javascript |
234 |
| -{ |
235 |
| - parentField: "__parent", |
236 |
| - optional: [], |
237 |
| - basedir: null, |
238 |
| - replaceEnv: "%", |
239 |
| - replaceLocal: "@", |
240 |
| - skipUnresolved: false |
241 |
| -} |
242 |
| -``` |
243 |
| - |
244 |
| -## Flow |
245 |
| - |
246 |
| -Flow of the configuration loader: |
247 |
| - |
248 |
| -1. Merge all configs passed in **path** parameter with all of their parents (merging all hierarchy) |
249 |
| -2. Merge all results to one json object |
250 |
| -3. Override configuration with environment variables |
251 |
| -4. Resolve environment variables |
252 |
| -5. Resolve local variables |
253 |
| - |
254 |
| -### Gulp commands: |
255 |
| - |
256 |
| -- `gulp checkstyle` - runs jshint and jscsrc analysis |
257 |
| -- `gulp test` - runs tests |
258 |
| -- `gulp test --file test/loader.js` - runs single test file `./test/loader.js` |
259 |
| -- `gulp` - alias for `gulp jshint test` |
260 |
| -- `gulp test-cov` - runs instrumented tests, generates reports to `./build/test` |
261 |
| -- `gulp test-cov --file test/loader.js` - runs single instrumented test file `./test/loader.js` |
262 |
| -- `gulp clean` - removes `./build` folder |
263 |
| -- `gulp ci` - alias for `gulp clean checkstyle test-cov` |
264 |
| - |
265 |
| -### NPM commands: |
266 |
| - |
267 |
| -- `npm test` - alias for `gulp test` |
268 |
| -- `npm run ci` - alias for `gulp ci` |
0 commit comments