I have two modules:
var client = require('./handlers/client.js');
var server = require('./handlers/server.js');
server.createClient()
client.js
var client = function(){
console.log("New client");
}
exports = module.exports = client;
server.js
var server = {
createClient: function() {
var newClient = new client();
}
}
exports = module.exports = server;
Doing this, the server module says that the client function is undefined.
How can I get this to work?
If you want to call client() in server.js, you will have to include this line:
var client = require('./client.js');
in your server.js file so that client is defined there, so the whole sequence in server.js would look like this:
var client = require('./client.js');
var server = {
createClient: function() {
var newClient = new client();
}
}
exports = module.exports = server;
You have to require() every module used within each module.
The module structure is meant for each module to stand on its own. That means that each module has its own separate namespace and you do not, by default, have access to the namespace of other modules. So, when you need access to anything from another module, you must either use require() to get access to it's namespace or you must call a function in another module (that you have required() in) and get access via that module.