2

is it possible to export automatically all the functions defined in a module of node.js?

Example:

Let's say that in the file foobar.js I have the following two functions

function foo() {
    ...
}

function bar() {
    ...
}

Does exist a tool for adding them automatically to the exports dictionary so that the following code is made possible?

foobar = require('foobar.js')
foobar.foo(); // works, even if not explicitly added in 'exports'
foobar.bar(); // also works

In other words, I would like to add progressively new functions to the module and not having to add them manually to the export dictionary. So, the following is what I want to avoid:

exports.foo = foo;
exports.bar = bar;

Maybe this could be achieved by getting introspectively the list of all the functions defined in the module, but I also don't know how to achieve this.

1 Answer 1

1

A way to expose things in a module:

var foobar = function(x, y) {
  this.x = x;
  this.y = y;
};
module.exports = foobar;

In your case:

foobar = require('foobar.js');
module.exports = foobar;

You can read extra here.

edit

doing what you need would require to overload node "require" method and this is not possible, take a look here.
Maybe you can install node-overload and use method RePrototype.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you for the answer, but what I meant is a way to add automatically the functions defined in the module to the exports
last chance: try node-overload

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.