nconf icon indicating copy to clipboard operation
nconf copied to clipboard

env doesn't append array values to default

Open acceleratxr opened this issue 3 years ago • 2 comments

nodejs version: all nconf version: v0.11.3

Steps to Reproduce

  1. 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"]
});
  1. 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'
// }

acceleratxr avatar Nov 10 '21 22:11 acceleratxr

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' }}

LongLiveCHIEF avatar Sep 14 '22 14:09 LongLiveCHIEF

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.

ollymg avatar Jan 30 '23 14:01 ollymg