18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

requireのキャッシュを消す

Posted at

requireは、キャッシュされていれば、キャッシュを返して、キャッシュされていなければ、ロードします。
キャッシュを削除してから、requireを実行すれば、再度ロードされます。
require.cacheに指定する値は、ファイル名。requireで指定した値ではない。

main.js
var sys = require('sys');

function sleep(time, callback) {
	setTimeout(callback, time);
}

function p() {
	delete require.cache[__dirname + '/config.js']
	var config = require(__dirname + '/config');
	sys.print(config.PORT + '\n');
	sleep(3000, p);
}

sleep(3000, p);

config.js
var config = {
	"PORT": 3302
}

module.exports = config;
18
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?