0

Currently I'm using this pattern in code:

module.exports.getMySQL = () => {
  return process.env.CLEARDB_DATABASE_URL || config.get('MySQL').connection;
}

however, node-config claims to be able to integrate these variables into a file as such.

https://github.com/lorenwest/node-config/wiki/Environment-Variables#custom-environment-variables

{
  "Customer": {
    "dbConfig": {
      "host": "PROD_SERVER"
    },
    "credit": {
      "initialDays": "CR_ID"
    },
    // Environment variables containing multiple configs
    // New as of config@1.14.0
    "settings": {
      "adminAccounts": {
        "__name": "ADMIN_ACCS",
        "__format": "json"
      }
    }
  }
}

What exactly is "PROD_SERVER"

If I replace this with "process.env.SOME_ENVIRONMENT_VARIABLE", it does not work and my server crashes.

I did verify that "process.env.SOME_ENVIRONMENT_VARIABLE" exists using the Heroku GUI.

2 Answers2

0

PROD_SERVER can be a name of the database server that you are trying to connect, if you need to read it from the Heroku configuration you need to set the configurations in heroku.

Lets say you set an environment variable with the name "DATABASE_URL" in heroku you can access it in your code as process.env.DATABASE_URL.

The environment variables for your Heroku app will be available in the settings tab of your app. Under "Config Variables"

Your server crashes when you use process.env.SOME_ENVIRONMENT_VARIABLE because the value is not set in configuration, you can see your Heroku app's console for more details.

Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50
  • Sorry I was not more clear. I did set this value using Heroku console. –  Mar 19 '18 at 05:34
0

I'm one of the maintainers of node-config.

PROD_SERVER in the docs is an example of an environment variable name.

You would place SOME_ENVIRONMENT_VARIABLE in custom-environment-variables.json, but don't put process.env.SOME_ENVIRONMENT_VARIABLE in the JSON file.

From the example custom-environment-variables.json file you've provided, you could then use config.get('Customer.dbConfig.host') and this would reference the value you set in the PROD_SERVER environment variable.

Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • I'm just curious as to why? I'm trying to understand the use case for `custom-environment-variables.json`. Those are Heroku environment variables but you are saying to point to them in that file. Can you give me a more concrete example of when I would use `custom-environment-variables.json`. The functionality I have now is fine, I was just hoping to clean my code up a bit and I would have preferred to use the file mentioned above for consistency. –  Mar 19 '18 at 21:51
  • Updated answer with more explicit example. – Mark Stosberg Mar 20 '18 at 13:56
  • Do you know where in Heroku I would set PROD_SERVER? –  Mar 20 '18 at 22:15
  • @BrannonTeeer https://devcenter.heroku.com/articles/config-vars Set them as Config Vars. – Mark Stosberg Mar 21 '18 at 15:26
  • Hi Mark, sorry to bump this, but I've set up the custom-environment-variables.json file as in your example, and it works fine locally, but Heroku always defaults to either the NODE_ENV json file or the default.json. If you've got the time I'd really appreciate your help https://stackoverflow.com/questions/54414294/node-config-wont-use-variables-from-custom-environment-variables-json-in-heroku – NickW Jan 29 '19 at 13:53