Can node modules require each other
I think there is much better way how to do it. Just switch export and require like this:
user.js
var User = function () {};
module.exports = User;
User.prototype.test = function () {
return new Room();
};
var Room = require('./room.js');
room.js
var Room = function () {};
module.exports = Room;
Room.prototype.test = function () {
return new User();
};
var User = require('./user.js');
index.js
var User = require('./user.js');
var Room = require('./room.js');
var user = new User();
var room = new Room();
user.test();
room.test();
check this article: https://coderwall.com/p/myzvmg/circular-dependencies-in-node-js
Check out http://nodejs.org/api/modules.html#modules_cycles for how this is handled in node.
You can solve your problem in several ways, for example passing in the dependencies to the instances aka Dependency Injection
// user.js
var User = function (Room) { this.Room = Room; };
User.prototype.test = function () {
return new this.Room();
};
module.exports = User;
// room.js
var Room = function (User) { this.User = User; };
Room.prototype.test = function () {
return new this.User();
};
module.exports = Room;
// index.js
var User = require('./user.js');
var Room = require('./room.js');
var user = new User(Room);
var room = new Room(User);
Another way would be to require the files only when you need them
// user.js
var User = function () {};
User.prototype.test = function () {
var Room = require('./room');
return new Room();
};
module.exports = User;
// room.js
var Room = function () {};
Room.prototype.test = function () {
var User = require('./user');
return new User();
};
module.exports = Room;
// index.js
var User = require('./user.js');
var Room = require('./room.js');
var user = new User();
var room = new Room();
Like this, your exports are defined by the time you need them.
But generally, if you have circular dependencies, you are doing something wrong and should think about your architecture.
If a User
needs to create new Rooms
and a Room
needs to create new Users
, it seems like they both have too much responsibility.
Possibly you would want a third component which is responsible for creating and passing the right instances to the Room
and User
, instead of having them instantiate those directly.