D
D
Delias2015-06-28 13:55:17
JavaScript
Delias, 2015-06-28 13:55:17

Nodejs is there an auto-search for a module in the project tree?

Tell me, here I have a module that will return different settings for the application. Looks like this:

var params = function(){
    var debugging = 0;

    return {
        debugging: debugging
    };
};

module.exports = params;

But the fact is that I need to include this module in files that are in different folders (from other modules), and therefore I have to call it from different places in different ways:
var params = require('./app/params');
// or
var params = require('./params');

Isn't there a way to call a module connection with auto-search through the folder tree, like npm does for example?
Or in this case, params is not a sin to throw in GLOBAL?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Shemsedinov, 2015-06-28
@MarcusAurelius

Autosearch is not needed, just imagine, what if he scans all folders with each call or what? At the start of the process, remember __dirnameor process.cwd()and then count from it. In general, the config should be read 1 time at startup and there is nothing wrong if you write it to the global scope: GLOBAL.config = require('config.js');
and from anywhere in the program you will access it as: config.debugging
Or you can write it inside an object that you already have global, for example: application.config = require('config.js');
And then generally beautiful to write: application.config.debugging
In other words: USE THE GLOBAL LUKE

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question