Ron Melkhior Ron Melkhior - 1 year ago 53
Node.js Question

Requiring a folder and using exports.{function}

Currently I have this setup:

// index.js
var example = require('./folder');


and:

// folder/index.js
require('./more');

// folder/test.js
exports.thing = function() {
console.log('test');

return true;
}


But when I try in
index.js
to call
example.thing
I get:

example.thing is not a function


Is there any way to make it work? Cheers.

Answer Source

Directory requiring is not supported by node. It requires index.js if it presented in the directory.

To export thing in index.js do the following:

// index.js
exports.thing = require('./test.js').thing;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download