S
S
Sirian2012-09-12 17:11:43
JavaScript
Sirian, 2012-09-12 17:11:43

Node.js: How bad is it to use a global object?

Node.js has a global object.
I would like to have some kind of bootstrap file that would read configs, parse command line parameters. Roughly speaking initialized entirely application.
in any of the files used inside, I want to be able to access the config. One of the solutions is to push some object into the global in the bootstrap file (for example, index.js), for example

global.app = {
    config: {...},
    somethingElse: {...}
}

Then inside any other file (especially with a large nesting) it will be possible to access app.config without all sorts of config = require('../../../../index.js').app.config;

How bad is this approach? And are there alternative solutions to have a minimum of dependencies on relative paths

Answer the question

In order to leave comments, you need to log in

2 answer(s)
1
1nd1go, 2012-09-12
@1nd1go

I think that this is a good practice if you have your object named as an application, etc., so that if anything, no one intersects with it.

U
unclechu, 2012-09-25
@unclechu

If any of the modules of your application wants to be reused (for you or someone else, and modular programming implies this), then problems may arise. Because something from your module will crawl into the global scope of all modules that are connected within the application, where your module is also connected. A very risky move in my opinion.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question