user3421904
user3421904

Reputation:

Exporting in Node

I have the following structure in my file1; now in file2 I just want to have access to "func1" not the other parts:

module.exports = function(_app) {
    .
    .
    .
}

function func1(param1) {
    .
    .
    .
}

module.exports.func1 = func1;

now in file2 when I have the following does it load just func1 or module.exports = function(_app) {} as well? If so how can I just use func1 in file2.

var file1 = require('file1');
file1.func1

Thanks; if you need more clarification please let me know...

Upvotes: 0

Views: 33

Answers (1)

Alnitak
Alnitak

Reputation: 339786

you can do:

var func1 = require('file1').func1;

Strictly speaking this imports the entire module, but discards the references to the other functions.

If you want to actually invoke the function that you've replaced module.exports with then you'd need to write this first:

require('file1')(app);  // invoke f(_app)

To do it in one line would require f(_app) to also return module.exports so you could chain the calls:

var func1 = (require('file1')(app)).func1;

Upvotes: 1

Related Questions