8

I keep getting this issue in Node where my application crashes whenever I'm calling functions from one another.

I've made this minimum working example (working as in it gives me the error):

Start module

var module2 = require('./module2');

var data = 'data';

module2.doStuff(data);

Module2

var module3 = require('./module3');

function doStuff(data){
    // Stuff happens to 'data'
    module3.takeStuff(data);
}

function doSomethingElse(data){
    console.log(data);
}


module.exports = {
    doStuff: doStuff,
    doSomethingElse: doSomethingElse
};

Module3

var module2 = require('./module2');

function takeStuff(data){
    // Stuff happens to 'data'
    module2.doSomethingElse(data); // I get the type error here
}

module.exports = {
    takeStuff: takeStuff
};

The error I get is:

module2.doSomethingElse(data); // I get the type error here
        ^
TypeError: undefined is not a function

The start module calls a function in module2 which ultimately calls for a function in module3, which in turn calls for a function in module2.

All modules are properly required and it finds the first method in module2 just fine.

What's happening here and how would one go about this pattern when one needs to get a function from the module one came with?

EDIT

Debugging shows me that the module exists, but it's empty apart from the prototype it has. My question is why? Inside of Node/JavaScript, what's happening here?

2 Answers 2

6

This issue here can be easily fixed, while keeping the structure of your app (which is fine, in regard to circular references).

You just need need to keep the default exports object that is given to your modules by the system. Do not change it with module.exports = {...}.

The following should work:

Start module

var module2 = require('./module2');

var data = 'data';

module2.doStuff(data);

Module 2

var module3 = require('./module3');

exports.doStuff = function doStuff(data){
    // Stuff happens to 'data'
    module3.takeStuff(data);
};

exports.doSomethingElse = function doSomethingElse(data){
    console.log(data);
};

Module 3

var module2 = require('./module2');

exports.takeStuff = function takeStuff(data){
    // Stuff happens to 'data'
    module2.doSomethingElse(data); // I get the type error here
};

Explanation

I'll try to explain what happens from the first line of your starting point:

  1. In start.js, you require module2 and it is not loaded yet: the code from module2.js is executed
  2. In module2.js, you require module3 and it is not loaded yet: the code from module3.js is executed
  3. In module3.js, you require module2, and it is already loaded: the module2 variable now contains the exports object from module2.
  4. The rest of module3 is executed and you change its exports object with module.exports = {...}
  5. In module2.js, the module3 variable now contains the exports object from module3.
  6. The rest of module2.js is executed and you change its exports object with module.exports = {...}
  7. In start.js, the module2 variable now contains the exports object from module2.

The issue here is between points 3 and 6. Module3 is receiving the exports of module2 (3) before you change the reference to it (6). Using exports.method = ... solves the problem because the exports object never changes.

4
  • What is the difference between exports.functionToExport and what I did?
    – Gemtastic
    Aug 3, 2015 at 11:22
  • @JohnSnow I updated my answer with a tentative of explanation
    – Targos
    Aug 3, 2015 at 11:48
  • But when I debug, it enters into module2 and executes that code just fine, how could it do that is module2 is not loaded?
    – Gemtastic
    Aug 3, 2015 at 11:55
  • 1
    It is loaded, but it happens too late. When the code from module2 is executed, module3 already has a reference to its old (and empty) exports object.
    – Targos
    Aug 3, 2015 at 11:58
4

You've got a circular reference, module 2 depends on module 3 which depends on module 2 which depends on module 3 (and so on). So while trying to resolve module 3 for module 2, it cannot complete it because module 3 itself needs to be resolved first.

From https://nodejs.org/api/modules.html#modules_cycles:

When there are circular require() calls, a module might not have finished executing when it is returned.

In order to prevent an infinite loop, an unfinished copy of the a.js exports object is returned to the b.js module.

So the module is available, but incomplete (which explains that you only receive a prototype). Circular references are a code smell anyway, try to avoid it :)

However, if you put both module 2 and 3 as a dependency in your starting module, it should work anyway.

var module2 = require('./module2');
var module3 = require('./module3');

var data = 'data';

module2.doStuff(data);
6
  • "When there are circular require() calls, a module might not have finished executing when it is returned." Well there you have it. facepalms And now there's an SO question mentioning it.
    – Gemtastic
    Aug 3, 2015 at 9:19
  • @JohnSnow Yeah, i will put that quote in my answer to clarify :) Aug 3, 2015 at 9:21
  • 1
    @JohnSnow Would be nice if node threw a circular reference warning of some sort though. Glad to help! Aug 3, 2015 at 9:26
  • Maybe a ticket to wish for?
    – Gemtastic
    Aug 3, 2015 at 9:33
  • The latest addition you mention withe the 3rd module in the start one doesn't work for me.
    – Gemtastic
    Aug 3, 2015 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.