Simply: How can I make a require() of a require (), and then get data back to original with an exports of an exports?
Here is a practical example:
My hello.js file:
var text = "Hello world!"
exports.text
In the same folder, I have the foo.js file:
var hello = require("./hello.js")
exports.hello
Finally, my app.js file (also in the same folder):
var foo = require("./foo.js")
console.log(foo.hello.text)
I'm expecting it to return:
Hello world!
But instead, it returns an error:
/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)
Any help? This situation is not so tricky: I would like to group my scripts in a folder with a unique entry script, that will call functions in various other files.
Thanks in advance.
You dont set any values on the exports. You have to do something like exports.text = text otherwise the export has no value
hello.js
var text = "Hello world!";
exports.text = text;
foo.js file:
var hello = require("./hello.js");
exports.hello = hello;
app.js file
var foo = require("./foo.js");
console.log(foo.hello.text);