From 3ad82c335deff29a1db5a391f893dc6d2ff1e63a Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Tue, 7 Apr 2015 16:30:28 +0200 Subject: [PATCH] module: handle NODE_PATH in require('.') This commit restores the functionality of adding a module's path to NODE_PATH and requiring it with require('.'). As NODE_PATH was never intended to be used as a pointer to a module directory (but instead, to a directory containing directories of modules), this feature is also being deprecated in turn, to be removed at a later point in time. PR-URL: https://github.com/iojs/io.js/pull/1363 Fixes: https://github.com/iojs/io.js/issues/1356 Reviewed-By: Jeremiah Senkpiel Reviewed-By: Rod Vagg --- lib/module.js | 20 ++++++++++++++++++- test/parallel/test-require-dot.js | 16 +++++++++++++++ test/parallel/test-require-extensions-main.js | 8 +------- 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 test/parallel/test-require-dot.js diff --git a/lib/module.js b/lib/module.js index 719f8bd50c5716..a89f544e51f619 100644 --- a/lib/module.js +++ b/lib/module.js @@ -125,6 +125,11 @@ function tryExtensions(p, exts) { } +const noopDeprecateRequireDot = util.deprecate(function() {}, + "warning: require('.') resolved outside the package directory. " + + "This functionality is deprecated and will be removed soon."); + + Module._findPath = function(request, paths) { var exts = Object.keys(Module._extensions); @@ -169,6 +174,8 @@ Module._findPath = function(request, paths) { } if (filename) { + // Warn once if '.' resolved outside the module dir + if (request === '.' && i > 0) noopDeprecateRequireDot(); Module._pathCache[cacheKey] = filename; return filename; } @@ -205,12 +212,23 @@ Module._resolveLookupPaths = function(request, parent) { } var start = request.substring(0, 2); - if (start !== '.' && start !== './' && start !== '..') { + if (start !== './' && start !== '..') { var paths = modulePaths; if (parent) { if (!parent.paths) parent.paths = []; paths = parent.paths.concat(paths); } + + // Maintain backwards compat with certain broken uses of require('.') + // by putting the module's directory in front of the lookup paths. + if (request === '.') { + if (parent && parent.filename) { + paths.splice(0, 0, path.dirname(parent.filename)); + } else { + paths.splice(0, 0, path.resolve(request)); + } + } + return [request, paths]; } diff --git a/test/parallel/test-require-dot.js b/test/parallel/test-require-dot.js new file mode 100644 index 00000000000000..2551d88d2204e8 --- /dev/null +++ b/test/parallel/test-require-dot.js @@ -0,0 +1,16 @@ +var common = require('../common'); +var assert = require('assert'); +var module = require('module'); + +var a = require(common.fixturesDir + '/module-require/relative/dot.js'); +var b = require(common.fixturesDir + '/module-require/relative/dot-slash.js'); + +assert.equal(a.value, 42); +assert.equal(a, b, 'require(".") should resolve like require("./")'); + +process.env.NODE_PATH = common.fixturesDir + '/module-require/relative'; +module._initPaths(); + +var c = require('.'); + +assert.equal(c.value, 42, 'require(".") should honor NODE_PATH'); \ No newline at end of file diff --git a/test/parallel/test-require-extensions-main.js b/test/parallel/test-require-extensions-main.js index 2a102afc950f77..06d28ab30bddcf 100644 --- a/test/parallel/test-require-extensions-main.js +++ b/test/parallel/test-require-extensions-main.js @@ -1,10 +1,4 @@ var common = require('../common'); var assert = require('assert'); -require(common.fixturesDir + '/require-bin/bin/req.js'); - -var a = require(common.fixturesDir + '/module-require/relative/dot.js'); -var b = require(common.fixturesDir + '/module-require/relative/dot-slash.js'); - -assert.equal(a.value, 42); -assert.equal(a, b, 'require(".") should resolve like require("./")'); +require(common.fixturesDir + '/require-bin/bin/req.js'); \ No newline at end of file