From 712e75ce495d1abda1cc399fc653cb58f30dc385 Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Tue, 7 Apr 2015 23:32:54 +0200 Subject: [PATCH] Better module dir detection, add warning --- lib/module.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/module.js b/lib/module.js index d65b0c724369df..dfbf25f99971ea 100644 --- a/lib/module.js +++ b/lib/module.js @@ -169,6 +169,9 @@ Module._findPath = function(request, paths) { } if (filename) { + if (request === '.' && i > 0) { + console.error(`(node) warning: require('.') resolved to ${filename}`); + } Module._pathCache[cacheKey] = filename; return filename; } @@ -210,12 +213,15 @@ Module._resolveLookupPaths = function(request, parent) { if (parent) { if (!parent.paths) parent.paths = []; paths = parent.paths.concat(paths); - // For '.', put PWD at the front of the resolve paths - // TODO(silverwind): Treat '.' exactly the same as './' - if (request === '.') { - paths.splice(0, 0, path.resolve(request)); - } } + + // For '.', put the module's dir at the front of the resolve paths + // TODO(silverwind): Treat '.' exactly the same as './' + if (request === '.') { + paths.splice(0, 0, parent && parent.filename ? + path.dirname(parent.filename) : path.resolve(request)); + } + return [request, paths]; }