Skip to content

Instantly share code, notes, and snippets.

@dcneiner
Created July 2, 2013 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcneiner/5906901 to your computer and use it in GitHub Desktop.
Save dcneiner/5906901 to your computer and use it in GitHub Desktop.

Just a tiny gist to illustrate node shares required dependencies between different files that require the same module. Modifying underscore in the parent file modified it for the child files as well.

var _ = require( "underscore" );
module.exports = {
_: _,
run: function () {
console.log( _.amazing );
console.log( _.after );
}
}
var _ = require( "underscore" );
module.exports = {
_: _,
run: function () {
console.log( _.amazing );
console.log( _.after );
}
}
{
"name": "http-server",
"version": "0.0.1",
"author": "Doug Neiner <doug@dougneiner.com>",
"main": "./parent.js",
"dependencies" : {
"underscore" : "*"
},
"license": "MIT",
"engines": {
"node": ">=0.10"
}
}
var _ = require( "underscore" );
_.amazing = "Awesome";
var m1 = require( "./module1.js" );
var m2 = require( "./module2.js" );
_.after = "This too";
// Expects: "Awesome"
// Expects: "This too"
m1.run();
// Expects: "Awesome"
// Expects: "This too"
m2.run();
// Expects: true
console.log( m1._ === _ );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment