Ron Melkhior Ron Melkhior - 28 days ago 6
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

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;
Comments