-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
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: #1363 Fixes: #1356 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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."); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
silverwind
Author
Contributor
|
||
|
||
|
||
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]; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
The linter doesn't like this.. do we normally
\'
escape single quotes or something?