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 <fishrock123@rocketmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
This commit is contained in:
parent
431673ebd1
commit
3ad82c335d
@ -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) {
|
Module._findPath = function(request, paths) {
|
||||||
var exts = Object.keys(Module._extensions);
|
var exts = Object.keys(Module._extensions);
|
||||||
|
|
||||||
@ -169,6 +174,8 @@ Module._findPath = function(request, paths) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (filename) {
|
if (filename) {
|
||||||
|
// Warn once if '.' resolved outside the module dir
|
||||||
|
if (request === '.' && i > 0) noopDeprecateRequireDot();
|
||||||
Module._pathCache[cacheKey] = filename;
|
Module._pathCache[cacheKey] = filename;
|
||||||
return filename;
|
return filename;
|
||||||
}
|
}
|
||||||
@ -205,12 +212,23 @@ Module._resolveLookupPaths = function(request, parent) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var start = request.substring(0, 2);
|
var start = request.substring(0, 2);
|
||||||
if (start !== '.' && start !== './' && start !== '..') {
|
if (start !== './' && start !== '..') {
|
||||||
var paths = modulePaths;
|
var paths = modulePaths;
|
||||||
if (parent) {
|
if (parent) {
|
||||||
if (!parent.paths) parent.paths = [];
|
if (!parent.paths) parent.paths = [];
|
||||||
paths = parent.paths.concat(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];
|
return [request, paths];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
16
test/parallel/test-require-dot.js
Normal file
16
test/parallel/test-require-dot.js
Normal file
@ -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');
|
@ -2,9 +2,3 @@ var common = require('../common');
|
|||||||
var assert = require('assert');
|
var assert = require('assert');
|
||||||
|
|
||||||
require(common.fixturesDir + '/require-bin/bin/req.js');
|
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("./")');
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user