From ef2f0396a4340ca7d738b80e4a2da6fd7de8ba0f Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Sat, 11 Apr 2015 17:07:34 +0200 Subject: [PATCH] Warn once through util.deprecate, refactor --- lib/module.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/module.js b/lib/module.js index dfbf25f99971ea..3f1b43c53f0057 100644 --- a/lib/module.js +++ b/lib/module.js @@ -7,7 +7,6 @@ const assert = require('assert').ok; const fs = require('fs'); const path = require('path'); - // If obj.hasOwnProperty has been overridden, then calling // obj.hasOwnProperty(prop) will break. // See: https://github.com/joyent/node/issues/1707 @@ -125,6 +124,11 @@ function tryExtensions(p, exts) { } +const noopDeprecateRequireDot = util.deprecate(function() {}, + `warning: require('.') did resolve outside the package directory. ` + + `This functionality will be deprecated soon.`); + + Module._findPath = function(request, paths) { var exts = Object.keys(Module._extensions); @@ -169,9 +173,8 @@ Module._findPath = function(request, paths) { } if (filename) { - if (request === '.' && i > 0) { - console.error(`(node) warning: require('.') resolved to ${filename}`); - } + // Warn once if '.' resolved outside the module dir + if (request === '.' && i > 0) noopDeprecateRequireDot(); Module._pathCache[cacheKey] = filename; return filename; } @@ -215,11 +218,14 @@ Module._resolveLookupPaths = function(request, parent) { paths = parent.paths.concat(paths); } - // For '.', put the module's dir at the front of the resolve paths - // TODO(silverwind): Treat '.' exactly the same as './' + // Maintain backwards compat with certain broken uses of require('.') + // by putting the module's directory in front of the lookup paths. if (request === '.') { - paths.splice(0, 0, parent && parent.filename ? - path.dirname(parent.filename) : path.resolve(request)); + if (parent && parent.filename) { + paths.splice(0, 0, path.dirname(parent.filename)); + } else { + paths.splice(0, 0, path.resolve(request)); + } } return [request, paths];