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);
}
};
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.
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.
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);
};
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)
@imVinayPandya That's exactly what you should get.
You are calling functions one from another in an infinite recursion.
@ChALkeR ya i got it. thank you for your time.
@targos Hello targos. Could i know what is difference between 'exports' and 'module.exports' on circular dependency problem?
Most helpful comment
You have a circular dependency in your modules.
TestrequiresTest2which in turn requiresTest. 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 whenTest2goes to runone.oneFunction, it is trying to runundefinedrather than a function becauseoneis an empty object.