sails.jswaterline

sails.js Is it possible to set process.env variables from application?


I am trying to use npm module fb to use facebook api. The config file is located in the module and here is the snapshot of the same

var config = { };

// should end in /
config.rootUrl  = process.env.ROOT_URL                  || 'http://localhost:3000/';


config.facebook = {
    appId:          process.env.FACEBOOK_APPID          || '130243393813697',
    appSecret:      process.env.FACEBOOK_APPSECRET      || 'c82696768ae4ad8b63db874cb64eb558',
    appNamespace:   process.env.FACEBOOK_APPNAMESPACE   || 'nodescrumptious',
    redirectUri:    process.env.FACEBOOK_REDIRECTURI    ||  config.rootUrl + 'login/callback'
};

module.exports = config;

I don't wish to change the config file of the module since node_modules folder is kept in the gitignore list. For configuring the module to use my app's appId and appSecret, I need to set the process.env variables FACEBOOK_APPID and FACEBOOK_APPSECRET

I understand that it can be done while calling the sails lift but is it by any means possible to set these values inside the app so that I only have to call

sails lift

without any of those variables and those should be set automatically ? Or what is the best way to achieve what I am trying to do here ?


Solution

  • You should set the environment-variables outside of your App.

    Instead of sails lift you could also use node app.js. With that you can define environment-variables for your node-application with:

    $> FOO='bar' node app.js

    In your case:

    $> FACEBOOK_APPID='232322a22' FACEBOOK_APPSECRET='mysecrete' node app.js

    If you want to set this vars in your app (I wouldn't suggest that!) you could set them before including your npm-module. For example in your config/bootstrap.js:

    module.exports.bootstrap = function(cb) {
    
      process.env.FACEBOOK_APPID = "myvar";
      process.env.FACEBOOK_APPSECRET = "mysecrete";
      sails.facebook = require("yourmodule");
    
      cb();
    };