Call a "local" function within module.exports from another function in module.exports?


How do you call a function from within another function in a module.exports declaration?

Here's some simplified code.

In my app.js, I do the following:

var bla = require('./bla.js');

and inside of bla.js is

module.exports = {

  foo: function (req, res, next) {
    return ('foo');

  bar: function(req, res, next) {;


I'm trying to access the function foo from within the function bar, and I'm getting:

TypeError: Object # has no method 'foo'

If I change to just foo() I get:

ReferenceError: foo is not defined

5/5/2012 1:15:00 PM

Accepted Answer

I think I got it. I just changed to and it seems to be working.

If someone has a better or more proper method, please feel free to correct me.

5/5/2012 1:21:46 PM

You could declare your functions outside of the module.exports block.

var foo = function (req, res, next) {
  return ('foo');

var bar = function (req, res, next) {
  return foo();


module.exports = {
  foo: foo,
  bar: bar

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow