Reputation: 1282
When I require a module in the main app.js
file on an Express project, are those modules accessible from other modules? Sounds confusing, let me give an example:
In my app.js I have the following:
var iniparser = require('iniparser');
var config = iniparser.parseSync('./config.ini');
Which works fine if use config.port
within the app.js
file. But when trying to access config
from the routes.js
file, it's not available.
So my question is, is the scope limited to the module/file other modules are included in? And if I wanted to parse the config.ini
would I have to include the module in any module/file I intend to use the 'iniparser' module.
I hope I've not confused anyone.
Upvotes: 0
Views: 138
Reputation: 3120
The scope of a variable declared with
var xxx = yyy;
is limited to the module/file where it is declared (or the function where it is declared, if it is declared inside a function.)
In your case, you have to add the two lines in every file/module where you need it.
As an optimization, and to avoid parsing the config file several times, you could have a config.js file
var iniparser = require('iniparser');
module.exports = iniparser.parseSync('./config.ini'); // executed only once
And then require your config in all files/modules where it is needed
var config = require('./config');
console.log(config.port);
Upvotes: 1