Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling function in each other module is not working #2923

Closed
imVinayPandya opened this issue Sep 17, 2015 · 7 comments
Closed

calling function in each other module is not working #2923

imVinayPandya opened this issue Sep 17, 2015 · 7 comments
Labels
question Issues that look for answers.

Comments

@imVinayPandya
Copy link

i have three files: is not working,
when i am running index.js file its giving me an error in file Test2.js TypeError: one.oneFunction is not a function

index.js File:

var test = require('./Test');
test.oneFunction(' from index ');

Test.js File:

var two = require('./Test2');
module.exports = {

    oneFunction: function(from) {
        two.twoFunction(' one ');
        console.log('function two from '+from);
    }
};

Test2.js File:

var one = require('./Test');
module.exports = {

    twoFunction: function(from) {
        one.oneFunction(' two ');
        console.log('function one from '+from);
    }
};
@Trott
Copy link
Member

Trott commented Sep 17, 2015

You have a circular dependency in your modules. Test requires Test2 which in turn requires Test. As explained in the documentation, when you do something like that, the second module (Test2) gets an incomplete copy of the first module (Test). So when Test2 goes to run one.oneFunction, it is trying to run undefined rather than a function because one is an empty object.

@Trott Trott added the question Issues that look for answers. label Sep 17, 2015
@Trott
Copy link
Member

Trott commented Sep 17, 2015

By the way, you can "fix" this code by moving var two = require('./Test2'); to the bottom of Test.js. This way, by the time you load Test2, the function you are calling in Test2 has already been exported in Test so it has access to it. Once you make that change, running index.js results in RangeError: Maximum call stack size exceeded which is probably more like the error you were expecting.

@Trott Trott closed this as completed Sep 17, 2015
@targos
Copy link
Member

targos commented Sep 17, 2015

There is another way to fix the circular dependency issue without changing the order of statements.
You just need to keep the original exports object:

index.js File:

var test = require('./Test');
test.oneFunction(' from index ');

Test.js File:

var two = require('./Test2');
exports.oneFunction = function(from) {
    two.twoFunction(' one ');
    console.log('function two from '+from);
}

Test2.js File:

var one = require('./Test');
exports.twoFunction = function(from) {
    one.oneFunction(' two ');
    console.log('function one from '+from);
};

@imVinayPandya
Copy link
Author

both of the example i have tried. @targos @Trott, i am trying solution that @Trott has provided. if i'll get solution than i will let you know.

// Hello, and welcome to hacking node.js!

RangeError: Maximum call stack size exceeded
at process.nextTick (node.js:465:22)
at onwrite (_stream_writable.js:335:15)
at WritableState.onwrite (_stream_writable.js:89:5)
at Socket._writeGeneric (net.js:684:5)
at Socket._write (net.js:694:8)
at doWrite (_stream_writable.js:292:12)
at writeOrBuffer (_stream_writable.js:278:5)
at Socket.Writable.write (_stream_writable.js:207:11)
at Socket.write (net.js:618:40)
at Console.log (console.js:36:16)

@ChALkeR
Copy link
Member

ChALkeR commented Sep 17, 2015

@imVinayPandya That's exactly what you should get.
You are calling functions one from another in an infinite recursion.

@imVinayPandya
Copy link
Author

@ChALkeR ya i got it. thank you for your time.

@kimzerokim
Copy link

@targos Hello targos. Could i know what is difference between 'exports' and 'module.exports' on circular dependency problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants