1
votes

Node-config's custom-environment-variables.json doesn't seem to be overriding values in default.json when NODE_ENV is set to 'test.' Here's an extremely simple config:

config/default.json:

{
  "jwtPrivateKey": "defaultkey"
}

config/custom-environment-variables.json:

{
 "jwtPrivateKey": "JWTPRIVATEKEY"
}

config/test.json:

{}

app.js:

console.log('NODE_ENV': + process.env.NODE_ENV);
console.log('Env: ' + process.env.JWTPRIVATEKEY);
console.log("Config: " + config.get('jwtPrivateKey'));

This works as expected: I get NODE_ENV=undefined (so node-config will default to 'development'), & the next two lines both print the correct value set in the environment variable. However, if I try to run a simple test with Jest & print the same:

tests/some.test.js:

test('Some Test', () => {
    console.log('NODE_ENV:' + process.env.NODE_ENV);
    console.log('Env: ' + process.env.JWTPRIVATEKEY);
    console.log("Config: " + config.get('jwtPrivateKey'));
});

I get NODE_ENV=test, Env=the correct value set in the environment variable...but Config=defaultKey.

In other words: although it shows that the environment variable is set to the right value, node-config doesn't seem to be pulling it in through custom-environment-variables.json.

Is this a bug? I've scoured the documentation but been unable to find any reason for this discrepancy.

2

2 Answers

2
votes

Do you by chance have a local.json file in your config folder? If so, this is a special file type that overrides everything, so your values may match this config's values exactly. The NODE_ENV var seemingly doesn't affect whether or not local.json is loaded last.

I just ran into this issue and temporarily renaming/removing the local.json solved my problem.

0
votes

I was also confused about this and search all different platforms and finally got this link from the reported issue of node-config.

enter image description here

dont use local