Skip to content

Commit 82790d8

Browse files
MylesBorinsjasnell
authored andcommitted
module: revert #3384 DEP0019 EOL
The original commit was landed without running CITGM. Unfortunately this change breaks the module `d` which has over 500k downloads a day. It is worth mentioning that the compatibility hack can be removed without breaking anything. We should definitely revisit for the next Semver-Major but shipping this today will cause non trivial ecosystem breakages. Refs: #3384 PR-URL: #16634 Refs: #3384 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent dfcaf28 commit 82790d8

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

doc/api/deprecations.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ code.
202202
<a id="DEP0019"></a>
203203
### DEP0019: require('.') resolved outside directory
204204

205-
Type: End-of-Life
205+
Type: Runtime
206206

207207
In certain cases, `require('.')` may resolve outside the package directory.
208208
This behavior is deprecated and will be removed in a future major Node.js

lib/module.js

+25-1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ function tryExtensions(p, exts, isMain) {
176176
return false;
177177
}
178178

179+
var warned = false;
179180
Module._findPath = function(request, paths, isMain) {
180181
if (path.isAbsolute(request)) {
181182
paths = [''];
@@ -232,6 +233,18 @@ Module._findPath = function(request, paths, isMain) {
232233
}
233234

234235
if (filename) {
236+
// Warn once if '.' resolved outside the module dir
237+
if (request === '.' && i > 0) {
238+
if (!warned) {
239+
warned = true;
240+
process.emitWarning(
241+
'warning: require(\'.\') resolved outside the package ' +
242+
'directory. This functionality is deprecated and will be removed ' +
243+
'soon.',
244+
'DeprecationWarning', 'DEP0019');
245+
}
246+
}
247+
235248
Module._pathCache[cacheKey] = filename;
236249
return filename;
237250
}
@@ -334,7 +347,8 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
334347
}
335348

336349
// Check for relative path
337-
if (request.charCodeAt(0) !== 46/*.*/ &&
350+
if (request.length < 2 ||
351+
request.charCodeAt(0) !== 46/*.*/ ||
338352
(request.charCodeAt(1) !== 46/*.*/ &&
339353
request.charCodeAt(1) !== 47/*/*/)) {
340354
var paths = modulePaths;
@@ -345,6 +359,16 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
345359
paths = parent.paths.concat(paths);
346360
}
347361

362+
// Maintain backwards compat with certain broken uses of require('.')
363+
// by putting the module's directory in front of the lookup paths.
364+
if (request === '.') {
365+
if (parent && parent.filename) {
366+
paths.unshift(path.dirname(parent.filename));
367+
} else {
368+
paths.unshift(path.resolve(request));
369+
}
370+
}
371+
348372
debug('looking for %j in %j', request, paths);
349373
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
350374
}

test/parallel/test-require-dot.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ const b = require(fixtures.path('module-require', 'relative', 'dot-slash.js'));
1010
assert.strictEqual(a.value, 42);
1111
assert.strictEqual(a, b, 'require(".") should resolve like require("./")');
1212

13-
// require('.') should not lookup in NODE_PATH
1413
process.env.NODE_PATH = fixtures.path('module-require', 'relative');
1514
m._initPaths();
16-
assert.throws(() => { require('.'); }, Error, "Cannot find module '.'");
15+
16+
const c = require('.');
17+
18+
assert.strictEqual(c.value, 42, 'require(".") should honor NODE_PATH');

0 commit comments

Comments
 (0)