12

I am running config@1.30.0 and I am attempting to get settings from the environment variables using .\config\custom-environment-variables.json does not work. However, it reads from the .\config\default.json just fine.

.\config\custom-environment-variables.json

{
  "key": "app_key"
}

.\config\default.json

{
  "key": "defaultKey"
}

running

const config = require('config');
console.log(config.get('key'))

always prints

defaultKey

but prints nothing when I set the key property in config/default to an empty string. How can I resolve this?

What I have tried

  1. Opened a new console anytime I set the environment variable using set app_key=newKey
  2. Set the environment manually
otorrillas
  • 4,357
  • 1
  • 21
  • 34
djangbahevans
  • 153
  • 1
  • 2
  • 6

6 Answers6

16

The config file name relates to the NODE_ENV environment variable you use when starting node.

The purpose of the module is to have a config file for each type of environment you are deploying to test, staging, prod environments. Default takes over if nothing is set or a file can't be find

e.g. for test and staging environments you would have.

config/default.json

{
  "key": "default_key"
}

config/test.json

{
  "key": "test_key"
}

config/production.json

{
  "key": "prod_key"
}

app.js

var config = require('config')
console.log(config.key)

Then if you run with a different NODE_ENV the same name as the file in the config directory you get the different keys

node app.js // output default_key
NODE_ENV=test node app.js // output test_key
NODE_ENV=production node app.js // output prod_key

You question references custom environment variables using the file config/custom-environment-variables.json This file will enable you to override a value in one of the files with a environment variable set when running node. This is useful when you can't commit the variable, such as database key but might want to access all your config in the same place.

e.g.

{
  "key": "SECURE_DATABASE_KEY"
}

Then running the same program again with the new config file:

NODE_ENV=production node app.js // output prod_key
SECURE_DATABASE_KEY=asldfj40 NODE_ENV=production node app.js // output asldfj40
Peter Grainger
  • 4,539
  • 1
  • 18
  • 22
  • Thank you, but I read in the [documentations](https://github.com/lorenwest/node-config/wiki/Environment-Variables#custom-environment-variables) that, it's possible to create a custom environment variable so long as you stored the environment variable name in a file named `custom-environment-variables.json` inside the `config` folder. I am trying to do that but it does not seem to work. – djangbahevans Jun 01 '18 at 01:41
  • didn't see that documentation, I've changed my answer to include an example of that. Tested it out and works fine, might use it on my stuff – Peter Grainger Jun 01 '18 at 13:03
  • I also like to mention setting the environment variable On Mac: export SECURE_DATABASE_KEY=yourSecureKey On Windows: set SECURE_DATABASE_KEY=yourSecureKey And then npm start or npm run dev or whatever will do just fine on its own. – Muhammad Mubashirullah Durrani Aug 22 '23 at 05:27
8

I ran into a similar problem and found that if I don't open a new terminal window and I restart the server in the same window where I run export some_secret=immasecret, then the app doesn't crash and the some_secret variable can be accessed. I'd previously been trying to access the variable while running node in another window.

Ginnie Hench
  • 277
  • 2
  • 11
  • After posting this, I switched to using dotenv instead of the config module so I don't have to worry about what happens when I switch terminals. There are probably other solutions, but I like dotenv. – Ginnie Hench Jun 09 '18 at 22:23
5

This issue is with VSCODE Editors Integrated Terminal

We have also struggled a lot with this issue initially, but the issue is that you might be using the integrated terminal that comes with VSCODE there is an issue with that, please try to use some external terminals like cmder or cmd prompt that comes with windows you will get the output as you are expecting.

USE EXTERNAL TERMINAL OR CMD PROMPT to execute the code

A.L
  • 10,259
  • 10
  • 67
  • 98
Detroit Charan
  • 161
  • 1
  • 2
2

A solution is custom-env nodejs module, it allows you to add different environment variables for different stages using the popular .env method. Example .env for dev environment and .env.staging for staging environment

Erisan Olasheni
  • 2,395
  • 17
  • 20
0

Your files and codes are correct your cmd command is wrong

use this command

setx app_key NewKey

  • Please, expand your answer and explain why it works, including the notable sections of the documentation that explain why OP should choose this solution. – Luca Cappelletti Jul 14 '18 at 08:48
0

Attention!

If config/production.json

{
  "key": "prod_key"
}

and config/local.json

{
  "key": "local_key"
}

and

NODE_ENV=production node app.js

the Output is: local_key

If a local.json exist is NODE_ENV=production is ignored

Details s. config Wiki (it is very refined, unfortunately too few examples)

Gerd
  • 2,265
  • 1
  • 27
  • 46