5

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.

J23
  • 3,061
  • 6
  • 41
  • 52

3 Answers3

3

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.

adrum
  • 153
  • 1
  • 8
  • Nope, no local.config. Thanks for the idea tho :) – J23 May 29 '19 at 15:35
  • Dang! Feel free to check the load order to make sure the your files match the load order: https://github.com/lorenwest/node-config/wiki/Configuration-Files#file-load-order. It feels like to me the contents of `config/custom-environment-variables.json` should actually be in `test.json`. Edit: Never mind, I read the docs and understand what you're trying to do. I will report back if I find anything. – adrum May 30 '19 at 04:19
1

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

Mike
  • 1,048
  • 2
  • 11
  • 23
1

You must set env value before config module load

process.env.TargetEnv=1234 // <=== override default value
import config from 'config'
import config from 'config'
process.env.TargetEnv=1234 // <=== not override default value