From c452db66603e0c48d776dae81ec98101920f7d78 Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Wed, 18 Jan 2017 00:55:34 +0100 Subject: [PATCH 1/2] module: remove require('.') with NODE_PATH compat This removes the hack for the unintended use case of require('.') used in conjunction with NODE_PATH. NODE_PATH was never intended to be pointed to a module's directory directly, but instead should point to a directory containing modules in sub-directories, which can be loaded through require('module-name'); --- lib/module.js | 26 +------------------------- test/parallel/test-require-dot.js | 6 ++---- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/lib/module.js b/lib/module.js index 339a228da91bdf..7bb8288f54f782 100644 --- a/lib/module.js +++ b/lib/module.js @@ -159,7 +159,6 @@ function tryExtensions(p, exts, isMain) { return false; } -var warned = false; Module._findPath = function(request, paths, isMain) { if (path.isAbsolute(request)) { paths = ['']; @@ -221,18 +220,6 @@ Module._findPath = function(request, paths, isMain) { } if (filename) { - // Warn once if '.' resolved outside the module dir - if (request === '.' && i > 0) { - if (!warned) { - warned = true; - process.emitWarning( - 'warning: require(\'.\') resolved outside the package ' + - 'directory. This functionality is deprecated and will be removed ' + - 'soon.', - 'DeprecationWarning', 'DEP0019'); - } - } - Module._pathCache[cacheKey] = filename; return filename; } @@ -335,8 +322,7 @@ Module._resolveLookupPaths = function(request, parent, newReturn) { } // Check for relative path - if (request.length < 2 || - request.charCodeAt(0) !== 46/*.*/ || + if (request.charCodeAt(0) !== 46/*.*/ && (request.charCodeAt(1) !== 46/*.*/ && request.charCodeAt(1) !== 47/*/*/)) { var paths = modulePaths; @@ -347,16 +333,6 @@ Module._resolveLookupPaths = function(request, parent, newReturn) { 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.unshift(path.dirname(parent.filename)); - } else { - paths.unshift(path.resolve(request)); - } - } - debug('looking for %j in %j', request, paths); return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]); } diff --git a/test/parallel/test-require-dot.js b/test/parallel/test-require-dot.js index e2202efec1df1a..2e8a5c79f6e14a 100644 --- a/test/parallel/test-require-dot.js +++ b/test/parallel/test-require-dot.js @@ -10,9 +10,7 @@ const b = require(fixtures.path('module-require', 'relative', 'dot-slash.js')); assert.strictEqual(a.value, 42); assert.strictEqual(a, b, 'require(".") should resolve like require("./")'); +// require('.') should not lookup in NODE_PATH process.env.NODE_PATH = fixtures.path('module-require', 'relative'); m._initPaths(); - -const c = require('.'); - -assert.strictEqual(c.value, 42, 'require(".") should honor NODE_PATH'); +assert.throws(() => { require('.'); }, Error, "Cannot find module '.'"); From c72dbea1e69c70192eb6ef8284944e917986fc99 Mon Sep 17 00:00:00 2001 From: Roman Reiss Date: Wed, 30 Aug 2017 20:08:14 +0200 Subject: [PATCH 2/2] doc: mark DEP0019 as End-of-Life --- doc/api/deprecations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index c34f4e04d97923..5a0e1a898c70c1 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -201,7 +201,7 @@ code. ### DEP0019: require('.') resolved outside directory -Type: Runtime +Type: End-of-Life In certain cases, `require('.')` may resolve outside the package directory. This behavior is deprecated and will be removed in a future major Node.js