Skip to content

Instantly share code, notes, and snippets.

@bendrucker
Last active August 29, 2015 14:03
Show Gist options
  • Save bendrucker/82beed49ab13d037f107 to your computer and use it in GitHub Desktop.
Save bendrucker/82beed49ab13d037f107 to your computer and use it in GitHub Desktop.
Node module caching
var count = 0;
count++;
module.exports = count;
require('./counter') // => 1
require('./counter') // => 1
// count reference does not exist yet
// resolve and initialize the counter module
// it creates a counter reference as 0, increments it, and exports it
require('./counter');
// retrieve the cached counter module
// count reference is unchanged because counter is not re-evaluated
require('./dep1');
require('./dep2');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment