Q:

Access to functions in required modules defined in the calling module

How can you call a function within a required module but defined in the main program?
Main.js:
var http = require('http');
var aFunc = function() {return 1;}
var bFunc = require('./bFunc').bFunc;
var cFunc = function() {console.log(aFunc() + bFunc()); } // should equal 3.

httpServer = http.createServer(cFunc()).listen(80);

bFunc.js:
var bFunc = function() {return aFunc() + aFunc()};
exports.bFunc = bFunc;

I don't want to define (copy) aFunc in bFunc, nor do I want to put aFunc into its own file (aFunc.js).  Is there a way to access aFunc in bFunc by referring back to the main program that "required" bFunc?

A:

You'd have to pass afunc to bfunc:
// main.js
var cFunc = function() {console.log(aFunc() + bFunc(afunc)); }

// bfunc.js
var bFunc = function(f) {return f() + f()};

JavaScript has lexical scope.

