Obtain all functions in a file in Node.js

Problem

I have some functions inside a file. I’m trying to obtain all functions in that file, from within that file. Normally, all functions are in the window object, but I'm using Node.js, which does not seem to have a window object.

Say I have something along the lines of the following in a file:

function foo() {}
function bar() {}

then:

  1. Are the functions saved in some global object?
  2. If not, how can I access these functions without knowing their names? Can I iterate through all existing functions and obtain them in such a way?

Problem courtesy of: pimvdb

Solution

The following is a common pattern

var foo = exports.foo = function() {
// ...
}

This way its written to exports and you can access it locally as foo

Solution courtesy of: Raynos

View additional discussion.