require()
簡単に言うと、 を で作成し、でrequire ()
データを元に戻すにexports
はどうすればよい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)
何か助けはありますか?この状況はそれほどトリッキーではありません。さまざまな他のファイルの関数を呼び出す一意のエントリ スクリプトを含むフォルダーにスクリプトをグループ化したいと考えています。
前もって感謝します。