Question

I'm having the following 3 files.

user.js requires room.js and room.js requires user.js.

user.js

var Room = require('./room.js');

var User = function () {};
User.prototype.test = function () {
  return new Room();
};

module.exports = User;

room.js

var User = require('./user.js');

var Room = function () {};
Room.prototype.test = function () {
  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();

user.test();
room.test();

index.js requires both room and user.

Here's the problem. When I run index.js, I will get a TypeError from 'new User()' in room.js. It seems that User in room.js is hidden by the User in index.js.

Am I doing anything wrong? Is this kind of requiring allowed? Any ideas? Thanks.

Was it helpful?

Solution

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.

OTHER TIPS

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

The difference between:

/* code above */
function a() {};

and

/* code above */
var a = function () {};

Is that in the first example a will be function already in the code above, but not in the second example.

Once you understand that you can come to this simple solution:

// user.js

module.exports = User;

function User() {};
User.prototype.test = function () {
  var Room = require('./room');
  return new Room();
};

// room.js

module.exports = Room;

function Room() {};
Room.prototype.test = function () {
  var User = require('./user');
  return new User();
};

// index.js

var User = require('./user.js');
var Room = require('./room.js');

var user = new User(Room);
var room = new Room(User);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top