nconf
nconf copied to clipboard
env doesn't append array values to default
nodejs version: all nconf version: v0.11.3
Steps to Reproduce
- Create an application that uses nconf with env parsing and the following defaults.
const conf = require("nconf")
.env({
separator: "__",
parseValues: true,
});
conf.defaults({
myarray: ["value1"]
});
- Launch the application with the following environment variable passed in.
myarray__1=value2
Expected Results
The resulting array has two values.
const myarray = conf.get("myarray");
// [
// 0: 'value1',
// 1: 'value2'
// ]
Actual Results
The result is transformed to an object with one value, value2
with key 1
. The first value of key 0
is missing and the object is not iterable.
const myarray = conf.get("myarray");
// {
// 1: 'value2'
// }
It doesn't append values period:
export FOO__HELLO=world
// config.json
{
"foo": {
"bar": "baz"
}
}
// program.js
nconf
.env({ inputSeparator: '__', lowerCase: true })
.file('config.json')
console.log(nconf.get('foo'))
expected:
{ foo: { bar: 'baz', hello: 'world' }}
received:
{ foo: { bar: 'baz' }}
I've found the following to work @acceleratxr @LongLiveCHIEF 👍. Although it is obviously undocumented.
Run:
a__a=1 a__b=false node main.js
main.js:
const nconf = require("nconf");
nconf.env({ parseValues: true, separator: "__" });
nconf.defaults({
a: { a: 3 },
});
console.log(nconf.get("a"));
output:
{ a: 1, b: false }
So in short when passing an object as config to the .env()
function it needs a key of separator
rather than inputSeparator
.
Interestingly nconf.env("__")
behaves correctly, but I needed additional config options.