NodeJS - config cannot not load custom environment variables
Asked Answered
P

6

12

I am running [email protected] 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
Provisional answered 31/5, 2018 at 7:26 Comment(0)
E
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
Extremadura answered 31/5, 2018 at 9:0 Comment(3)
Thank you, but I read in the documentations 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.Provisional
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 stuffExtremadura
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.Maneating
O
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.

Octavla answered 8/6, 2018 at 18:33 Comment(1)
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.Octavla
P
7

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

Prytaneum answered 12/4, 2020 at 8:44 Comment(0)
W
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

Weighting answered 28/3, 2019 at 9:28 Comment(0)
O
0

Your files and codes are correct your cmd command is wrong

use this command

setx app_key NewKey

Ollieollis answered 14/7, 2018 at 8:37 Comment(1)
Please, expand your answer and explain why it works, including the notable sections of the documentation that explain why OP should choose this solution.Wilmer
N
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)

Northumberland answered 11/8, 2021 at 17:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.