Node.js:递归请求和导出

Node.js:递归请求和导出,node.js,module,Node.js,Module,简单地说:我如何对require进行require,然后使用exports的exports将数据恢复到原始数据 以下是一个实际例子: 我的hello.js文件: var hello = require("./hello.js"); exports.hello = hello; 在同一个文件夹中,我有foo.js文件: var hello = require("./hello.js"); exports.hello = hello; 最后,我的app.js文件也在同一文件夹中: var foo

简单地说:我如何对require进行require,然后使用exports的exports将数据恢复到原始数据

以下是一个实际例子:

我的hello.js文件:

var hello = require("./hello.js");
exports.hello = hello;
在同一个文件夹中,我有foo.js文件:

var hello = require("./hello.js");
exports.hello = 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的操作,否则导出没有任何值

你好

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);

您不会在导出上设置任何值。您必须执行类似exports.text=text的操作,否则导出没有任何值

你好

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);

我想你得给出口命名我想你得给出口命名