简体   繁体   中英

Setting default environment variables in Heroku

I'm working on an app that connects to third-party APIs which require the use of an APP ID and SECRET KEY.

I am storing these values as environment variables in heroku, so that I don't need to expose them in my code.

If I deploy to heroku, it will use heroku's environment variables to resolve these API credentials.

If I'm working on it locally, I want to use my config.js module, and lookup the API credentials there. NOTE: This config.js file is included in my .gitignore so that these credentials never end up in the cloud.

The problematic code is this:

var api_secret = process.env.API_SECRET || require('../../config.js').secret;

When I run this locally, I've got no issues. Meaning, it is unable to resolve the environment variable, so instead it uses the secret from within config.js .

When I run it on heroku, it DOES throw an error telling me that module 'config.js' could not be found . This makes sense, because it was never pushed up with the rest of the repo, by virtue that it is in my .gitignore .

Because heroku is parsing through my code before it ever runs, the require('../../config.js') is problematic. It is trying to lookup a file that doesn't exist.

How can I solve the issue of using environment variables when deployed, and the config.js module when running locally?

On the Heroku dashboard for your application, you can set config variables. If you have the Heroku Toolbelt set up on your machine, you can also use:

heroku config:set API_SECRET=secret

See this article for more.

Edit: Think I may have misunderstood the question. I would suggest, if possible, using the dotenv npm package to set your config variables locally.

If not, another thing to check would be that the config.js package is in your package.json file, because Heroku will use this to build your dependencies.

If you do not want to push your config.js to heroky at all, you can just follow the following to determine whether the config file exists or not with a try catch and the file system module:

Check synchronously if file/directory exists in Node.js

In your case:

var fs = require('fs'),
    api_secret,
    config;

try {
    // Check whether config.js exists
    config = fs.lstatSync('../../config.js');
    // If we reach this line then config.js exists, yay!
    api_secret = process.env.API_SECRET || require('../../config.js').secret;
    // or alternatively  api_secret = require('../../config.js').secret; 
    // depending on your logic

}
catch (e) {
    // else config.js does not exist
    api_secret = process.env.API_SECRET
}

To run Heroku commands programmatically, you can set up a free Ruby app and make it do what you want through API calls. Use Heroku-api. See https://github.com/heroku/heroku.rb

If you want to set Heroku commands manually, you can set env variables on Heroku either with the command heroku config:set MYVAR=MYVALUE or through the Heroku dashboard (Click on your app > settings > reveal config vars > edit).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM