dskrepps / requireFrom

Require from a directory relative to node_modules, flattening your require paths.
MIT License
47 stars 5 forks source link

propagate the references #6

Open antonioaltamura opened 8 years ago

antonioaltamura commented 8 years ago

Hi, it would be very helpful if the module 'propagates' the variables defined in it in the node.js environment. Il'l explain better: in your base example you wrote

var requireFrom = require('requirefrom')
var views = requireFrom('lib/components/framework/views/')

but it would be VERY helpful if 'views' would be visible in all the other node.js scripts (obiouvsly requiring "requirefrom"). What do you think?

dskrepps commented 8 years ago

Perhaps you should instead consider Dependency Injection. This is when you write your modules as a function (or "factory") that encompasses the module's methods, and that factory accepts the dependencies (views in that example) as it's arguments.

Here are a couple simple examples: https://blog.risingstack.com/fundamental-node-js-design-patterns/#dependencyinjection http://stackoverflow.com/a/36771139

'views' in your question is an object like any other that can be injected into your modules, or assigned to some application object that is passed to all your modules.