Node-config custom-environment-variables not overridding default.json during test
Asked Answered
C

3

6

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.

Calle answered 10/5, 2019 at 21:2 Comment(0)
C
5

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.

Cass answered 29/5, 2019 at 4:15 Comment(3)
Nope, no local.config. Thanks for the idea tho :)Calle
Dang! Feel free to check the load order to make sure the your files match the load order: github.com/lorenwest/node-config/wiki/…. 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.Cass
I've spent the last 24 hours completely stumped and this answer solved my issues. I had made a local config to separate from the development config to be used on the dev server. Thanks you so much!!!! Almost 4 years later!!!!!Drucilladrucy
M
3

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
Maddie answered 30/3, 2022 at 8:31 Comment(1)
Thank you, that solved it. require('dotenv').config(); import config from 'config';Stepp
S
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

Sesquipedalian answered 24/1, 2021 at 19:12 Comment(1)
Is this different from the answer adrum gave in May 2019...?Calle

© 2022 - 2024 — McMap. All rights reserved.