I'm trying to pull an entire module into the current scope of another file after a require call.
For instance if my module looks like this:
exports.chai = require "chai"
exports.mocha = require "mocha"
exports.Q = require "q"
How can I then access the individual variables like they are global variables in the current file? So instead of doing something like requireVar.chai I can just type chai?
Thanks for any advice
Using coffeescript, I recommend you use the destructuring assignment feature:
{chai, mocha, Q} = require 'some_module'
// use chai, mocha and Q for good stuff
Can't you just do:
var requireVar = require('yourmod')
, chai = requireVar.chai
, mocha = requireVar.mocha
, Q = requireVar.Q
;
//then use chai, mocha and Q afterwards
chai.hoo();
mocha.mon();
Q.tip();
If you really, really wanted to do this, I suppose you could use with:
with (require('yourMod')) {
// use chai, mocha etc.
}
Note that no-one likes with however, especially Douglas Crockford. In reality using requireVar.chai is good; it removes any ambiguity as to where chai originates from, and makes it easy to understand the structure of your code/ modules.
If you get bored typing requireVar, make it really short; a.chai.