diff --git a/lib/module.js b/lib/module.js index ef0fad209ff9c8..452a5806d4aa7a 100644 --- a/lib/module.js +++ b/lib/module.js @@ -11,6 +11,7 @@ const path = require('path'); const internalModuleReadFile = process.binding('fs').internalModuleReadFile; const internalModuleStat = process.binding('fs').internalModuleStat; +const isWindows = process.platform === 'win32'; // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. @@ -191,7 +192,7 @@ Module._nodeModulePaths = function(from) { // note: this approach *only* works when the path is guaranteed // to be absolute. Doing a fully-edge-case-correct path.split // that works on both Windows and Posix is non-trivial. - var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//; + var splitRe = isWindows ? /[\/\\]/ : /\//; var paths = []; var parts = from.split(splitRe); @@ -460,29 +461,23 @@ Module.runMain = function() { }; Module._initPaths = function() { - const isWindows = process.platform === 'win32'; + modulePaths = []; - if (isWindows) { - var homeDir = process.env.USERPROFILE; - } else { - var homeDir = process.env.HOME; + var nodePath = process.env['NODE_PATH']; + if (nodePath) { + var nodePaths = nodePath.split(path.delimiter); + for (var i = 0; i < nodePaths.length; i++) { + if (nodePaths[i]) modulePaths.push(nodePaths[i]); + } } - var paths = [path.resolve(process.execPath, '..', '..', 'lib', 'node')]; - + var homeDir = isWindows ? process.env.USERPROFILE : process.env.HOME; if (homeDir) { - paths.unshift(path.resolve(homeDir, '.node_libraries')); - paths.unshift(path.resolve(homeDir, '.node_modules')); - } - - var nodePath = process.env['NODE_PATH']; - if (nodePath) { - paths = nodePath.split(path.delimiter).filter(function(path) { - return !!path; - }).concat(paths); + modulePaths.push(path.resolve(homeDir, '.node_modules')); + modulePaths.push(path.resolve(homeDir, '.node_libraries')); } - modulePaths = paths; + modulePaths.push(path.resolve(process.execPath, '..', '..', 'lib', 'node')); // clone as a read-only copy, for introspection. Module.globalPaths = modulePaths.slice(0);