La-D La-D - 5 months ago 7
Node.js Question

Error properties not defined when pointing to a local variable

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 ?

Answer
/* 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'
        }
    }
};
Comments