node.jsherokuenvironment-variablesnode-config

Node-config won't use variables from custom-environment-variables.json in Heroku


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


Solution

  • 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.