0

I have the following configuration file :

/* env.js */

ENV_TO_USE = [
"local"
];

// local; dev; rec; pre; prod
module.exports = {
    env_properties : {
        local : {
            root_url : "localhost",
            port : 3000,
            root_dir : "/home/user/project/"
        },
        dev : {
            root_url : "devdomain",
            port : 3000,
            root_dir : "/apps/project/",
        }
    },
    global_properties : {
        path_include :
        {
            PATH_EXPRESS : env_properties[ENV_TO_USE].root_dir + 'express'
        }
    }
};

And in another file, I want to print the 'PATH_EXPRESS' value :

/* test.js */

var env = require('./env.js');
console.log(env.global_properties.path_include.PATH_EXPRESS);

But when I launch the script with the command node test.js, I get the following error :

PATH_EXPRESS : env_properties[ENV_TO_USE].root_dir + 'express'
                       ^
ReferenceError: env_properties is not defined
at Object.<anonymous> (C:\cygwin64\home\user\project\env.js:23:21)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at Object.<anonymous> (C:\cygwin64\home\user\project\test.js:1:73)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)

I still want to keep one single file, and not creating a second file. How can I solve this problem ?

dng
  • 411
  • 1
  • 5
  • 18
  • Possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) – Shaunak D Jun 28 '16 at 09:29
  • Thanks, but the answer of @user1280859 it more suitable to me :) – dng Jun 28 '16 at 09:49

1 Answers1

1
/* env.js */
ENV_TO_USE = [
"local"
];

var env_properties = {
        local : {
            root_url : "localhost",
            port : 3000,
            root_dir : "/home/user/project/"
        },
        dev : {
            root_url : "devdomain",
            port : 3000,
            root_dir : "/apps/project/",
        }
    }

// local; dev; rec; pre; prod
module.exports = {
    env_properties : env_properties,
    global_properties : {
        path_include :
        {
            // and you have to specify which env you want to use
            PATH_EXPRESS : env_properties[0].root_dir + 'express'
        }
    }
};
/* No need to export the env_properties since it is included in the scope */
dng
  • 411
  • 1
  • 5
  • 18
Vladimir G.
  • 885
  • 7
  • 15