の「キャッシュ」セクションを読みますそれは、このようなrequire.cacheオブジェクトにキャッシュされているモジュールオブジェクトの削除:
delete require.cache[require.resolve('./mymodule.js')];
あなたが二回目用のモジュールを必要とする前にこれを行います。
ほとんどの場合、おそらくモジュールの最上位レベルのコードを1回だけ実行し、モジュールを必要とする場合は、そのモジュールがエクスポートするものにアクセスしたいだけです。
var myMod = require("./mymodule.js"); //the first time you require the
//mymodule.js module the top level code gets
//run and you get the module value returned.
var myMod = require("./mymodule.js"); //the second time you require the mymodule.js
//module you will only get the module value
//returned. Obviously the second time you
//require the module it will be in another
//file than the first one you did it in.
素晴らしい!早速のお返事ありがとうございます。 – atlantis
'初期化コードを何度も実行し、そのコードをエクスポートされたメソッドに投げるだけでいいのか説明したり、例を挙げることができますか?' –
@sudoモジュールの本体にコードを残すのではなく、エクスポートする関数。モジュールを必要とするモジュールは、初期化コードを再実行するためにこの関数を実行できます。 '' 'js var db = connect();ノードモジュールは常にシングルトンです: module.exportsは VS =デシベル '' ' は' ''のJS module.exportsはの=は、それを考えるための別の方法がある '' ' を接続します。あなたのシングルトンは、コンストラクタ/ファクトリを公開してインスタンスを作成できます。 – timoxley