I'm getting undefined when I try importing a method from another file. I have 3 files one calling the next.
File1:
const { methodFromFile2 } = require('./file2');
methodFromFile2('myParam');
File2:
const { methodFromFile3 } = require("../file3");
consoele.log(methodFromFile3); // undefined
function methodFromFile2(coolParam) {
consoele.log(methodFromFile3); // undefined
}
module.exports = { methodFromFile2 };
File3
function methodFromFile3(coolParam) {
...
}
module.exports = { methodFromFile3 };
In file2, the method methodFromFile3
is always undefined. But, when I move the required statement inside methodFromFile2
, it works. Why is it that way and is that the correct way of doing it?
If there are other require()
statements that you aren't showing us, then this is probably caused by a circular require()
where A requires B and B requires A. When you do that, one of the requires will end up getting an empty module object, not the one the module intends to return.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.