Node tries to resolve the current module path name and merges node_modules into each of its parent directories. [Source] .
You can override this method at the top of the project module and add some logic to exclude parent directories from the array of result paths.
//app.js <-- parent project module, should be added at the top var Module = require('module').Module; var nodeModulePaths= Module._nodeModulePaths; //backup the original method Module._nodeModulePaths = function(from) { var paths = nodeModulePaths.call(this, from); // call the original method //add your logic here to exclude parent dirs, I did a simple match with current dir paths = paths.filter(function(path){ return path.match(__dirname) }) return paths; };
inspired by this module
source share