From 492fc0d7524c0a5b546fb1a16f1b49bb4db73da8 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 19 Oct 2010 11:36:10 -0700 Subject: [PATCH] Split out modulePaths and defaultPaths This way, the "default" paths of ~/.node_libraries and {prefix}/lib/node are only checked *after* anything that the user has placed into the NODE_PATHS environ, or pushed onto require.paths. This makes require.paths a much more effective write-target, albeit slightly less useful as a read-target. However, given the existence of require.resolve(), this is less of an issue -- if you want to know what a module ID will map to, just ask that question and get an authoritative answer from the loading machinery. --- src/node.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/node.js b/src/node.js index afe07cdbe6..261a3590ad 100644 --- a/src/node.js +++ b/src/node.js @@ -126,15 +126,20 @@ var module = (function () { var pathModule = createInternalModule('path', pathFn); var path = pathModule.exports; - var modulePaths = [path.join(process.execPath, "..", "..", "lib", "node")]; - - if (process.env["HOME"]) { - modulePaths.unshift(path.join(process.env["HOME"], ".node_libraries")); + // The paths that the user has specifically asked for. Highest priority. + // This is what's hung on require.paths. + var modulePaths = []; + if (process.env.NODE_PATH) { + modulePaths = process.env.NODE_PATH.split(":"); } - if (process.env["NODE_PATH"]) { - modulePaths = process.env["NODE_PATH"].split(":").concat(modulePaths); + // The default global paths that are always checked. + // Lowest priority. + var defaultPaths = []; + if (process.env.HOME) { + defaultPaths.push(path.join(process.env.HOME, ".node_libraries")); } + defaultPaths.push(path.join(process.execPath, "..", "..", "lib", "node")); var extensions = {}; var registerExtension = removed('require.registerExtension() removed. Use require.extensions instead'); @@ -185,7 +190,7 @@ var module = (function () { var start = request.substring(0, 2); if (start !== "./" && start !== "..") { - return [request, modulePaths]; + return [request, modulePaths.concat(defaultPaths)]; } // Is the parent an index module?