3

I'm using the node-config module for mongo connection strings and jwt keys.

For example:

if(!config.get('jwtPrivateKey')){
    throw new Error('FATAL ERROR: jwtPrivateKey is not defined');

My custom-environment-variables.json file looks like:

{
    "jwtPrivateKey": "video-app_jwtPrivateKey",
    "db": "video-app_db"
}

default.json

{
    "db": "default db",
    "jwtPrivateKey": "default key"
}

production.json

{
    "db": "production db",
    "jwtPrivateKey": "production key"
}

long story short - although the environment variables are set in heroku, node-config doesn't look at the values set in custom-environment-variables.json. I can alter the NODE_ENV and get the relevant json file's hardcoded values, but the environment variables are never used, which seems to contradict the docs

NickW
  • 1,207
  • 1
  • 12
  • 52
  • Does this also happen in your local environment? looks like something it rewriting the variable env. – Fulvio Apr 19 '20 at 06:13

3 Answers3

1

You can config vars on this way in Heroku

Irvin Sandoval
  • 742
  • 7
  • 18
  • I think I've created the vars correctly. Typing `heroku config:get video-app_db` returns the variable as expected. However, using node-config in my code to `config.get('db')` returns the value from default or production.json rather than the `custom-environment-variables.json` file. It seems to be a problem with node-config i guess? I'll edit my title to reflect that – NickW Jan 29 '19 at 13:49
  • If you suspect the problem is with`node-config`, you can set the environment variable yourself (without Heroku) and see if it works. I believe you'll find it does. The problem appears specific to Heroku. – Mark Stosberg Mar 09 '19 at 19:16
1

I had the same issue. It took removing the hyphen from the keys for heroku to read from custom-environment-variables.

So custom-environment-variables.json:

{
    "jwtPrivateKey": "video-app_jwtPrivateKey",
    "db": "video-app_db"
}

Becomes:

{
    "jwtPrivateKey": "videoapp_jwtPrivateKey",
    "db": "videoapp_db"
}

Heroku docs:

Config var policies

  • Config var keys should use only alphanumeric characters and the underscore character (_) to ensure that they are accessible from all programming languages. Config var keys should not include the hyphen character.
  • Config var data (the combination of all keys and values) cannot exceed 32kb for each app.
  • Config var keys should not begin with a double underscore (__).
  • A config var’s key should not begin with HEROKU_ unless it is set by the Heroku platform itself.
Ron
  • 228
  • 3
  • 9
0

So, I had the same issue: I deployed my app, set environment variables in Heroku, but nothing worked (unlike my local machine where everything was fine).
Seems like the problem was that I accidentally added all the node_modules folder to git. It happened because I created a .gitignore file after the initial commit was made (if I get it right).

What I did, that worked for me: I removed node_modules from git and committed/pushed changes to Heroku:

git rm -rf node_modules

then commit and push.

This was the error message I got:

Error: /app/node_modules/bcrypt/lib/binding/bcrypt_lib.node: invalid ELF header

I spent two days trying to figure it out.
I hope it's helpful.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Rus.N
  • 1