要打破它,并把它简化一下:想象一下,我在我的项目中三个文件。一个main.js和两个模块:moduleA.js和moduleB.js。 main.js访问从moduleB.js调用函数的moduleA.js。现在moduleB.js发现它需要一个仅在moduleA.js中可用的信息。当然,moduleB.js试图访问moduleA.js中的一个函数,这个函数在技术上能够将这些信息提供给moduleB.js,但是却有一个错误。无法访问另一个模块的功能的NodeJS
这里是简化的代码。
main.js
var a = require("./moduleA.js");
console.log(a.respond());
moduleA.js
var b = require("./moduleB.js");
module.exports = {
respond: function(){
return b.returnAnswerForModuleA();
},
getSomeInformationOnlyAvailableInA: function(){
return "This is the information we need!";
}
};
moduleB.js
var a = require("./moduleA.js");
module.exports = {
returnAnswerForModuleA: function(){
return a.getSomeInformationOnlyAvailableInA()
}
};
以下是错误消息:
/Users/Tim/Code/ChatBot/test/moduleB.js:5
return a.getSomeInformationOnlyAvailableInA()
^
TypeError: a.getSomeInformationOnlyAvailableInA is not a function
at Object.module.exports.returnAnswerForModuleA (/Users/Tim/Code/ChatBot/test/moduleB.js:5:16)
at Object.module.exports.respond (/Users/Tim/Code/ChatBot/test/moduleA.js:5:18)
at Object.<anonymous> (/Users/Tim/Code/ChatBot/test/main.js:3:15)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
为什么我不能访问从moduleB.js moduleA.js?
重组我的代码不是一个真正的选择!
感谢您的帮助!
谢谢!有用! – HansMu158