Node.js:recursion的require和exports

简单地说:我怎样才能做一个require()require () ,然后通过exports数据回到原始状态?

这是一个实际的例子:

我的hello.js文件:

 var text = "Hello world!" exports.text 

在同一个文件夹中,我有foo.js文件:

 var hello = require("./hello.js") exports.hello 

最后,我的app.js文件(也在同一个文件夹中):

 var foo = require("./foo.js") console.log(foo.hello.text) 

我期待它回来:

 Hello world! 

但是相反,它会返回一个错误:

 /Users/Hassinus/www/node/test/app.js:2 console.log(foo.hello.text) ^ TypeError: Cannot read property 'text' of undefined at Object.<anonymous> (/Users/Hassen/www/node/test/app.js:2:22) at Module._compile (module.js:449:26) at Object.Module._extensions..js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9) 

任何帮助? 这种情况并不那么棘手:我想用一个唯一的条目脚本将我的脚本分组到一个文件夹中,这个脚本将调用各种其他文件中的函数。

提前致谢。

你不要在出口上设置任何值。 你必须做一些像exports.text = text否则导出没有价值

hello.js

 var text = "Hello world!"; exports.text = text; 

foo.js文件:

 var hello = require("./hello.js"); exports.hello = hello; 

app.js文件

 var foo = require("./foo.js"); console.log(foo.hello.text);