Skip to content

Commit 75007d6

Browse files
committed
module: mark DEP0019 as End-of-Life
In certain cases, `require('.')` could resolve outside the package directory. This behavior has been removed. PR-URL: #26973 Refs: #3384 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 666c67e commit 75007d6

File tree

3 files changed

+22
-45
lines changed

3 files changed

+22
-45
lines changed

doc/api/deprecations.md

+6-4
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,9 @@ code.
442442
### DEP0019: require('.') resolved outside directory
443443
<!-- YAML
444444
changes:
445+
- version: REPLACEME
446+
pr-url: https://github.com/nodejs/node/pull/26973
447+
description: Removed functionality.
445448
- version:
446449
- v4.8.6
447450
- v6.12.0
@@ -452,11 +455,10 @@ changes:
452455
description: Runtime deprecation.
453456
-->
454457

455-
Type: Runtime
458+
Type: End-of-Life
456459

457-
In certain cases, `require('.')` may resolve outside the package directory.
458-
This behavior is deprecated and will be removed in a future major Node.js
459-
release.
460+
In certain cases, `require('.')` could resolve outside the package directory.
461+
This behavior has been removed.
460462

461463
<a id="DEP0020"></a>
462464
### DEP0020: Server.connections

lib/internal/modules/cjs/loader.js

+10-36
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ const {
7070
CHAR_FORWARD_SLASH,
7171
CHAR_BACKWARD_SLASH,
7272
CHAR_COLON,
73-
CHAR_DOT,
7473
CHAR_UNDERSCORE,
7574
CHAR_0,
7675
CHAR_9,
@@ -306,7 +305,6 @@ function findLongestRegisteredExtension(filename) {
306305
return '.js';
307306
}
308307

309-
var warned = false;
310308
Module._findPath = function(request, paths, isMain) {
311309
if (path.isAbsolute(request)) {
312310
paths = [''];
@@ -375,18 +373,6 @@ Module._findPath = function(request, paths, isMain) {
375373
}
376374

377375
if (filename) {
378-
// Warn once if '.' resolved outside the module dir
379-
if (request === '.' && i > 0) {
380-
if (!warned) {
381-
warned = true;
382-
process.emitWarning(
383-
'warning: require(\'.\') resolved outside the package ' +
384-
'directory. This functionality is deprecated and will be removed ' +
385-
'soon.',
386-
'DeprecationWarning', 'DEP0019');
387-
}
388-
}
389-
390376
Module._pathCache[cacheKey] = filename;
391377
return filename;
392378
}
@@ -490,35 +476,23 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
490476
return (newReturn ? null : [request, []]);
491477
}
492478

493-
// Check for non-relative path
494-
if (request.length < 2 ||
495-
request.charCodeAt(0) !== CHAR_DOT ||
496-
(request.charCodeAt(1) !== CHAR_DOT &&
497-
request.charCodeAt(1) !== CHAR_FORWARD_SLASH &&
498-
(!isWindows || request.charCodeAt(1) !== CHAR_BACKWARD_SLASH))) {
499-
var paths = modulePaths;
500-
if (parent) {
501-
if (!parent.paths)
502-
paths = parent.paths = [];
503-
else
504-
paths = parent.paths.concat(paths);
505-
}
479+
// Check for node modules paths.
480+
if (request.charAt(0) !== '.' ||
481+
(request.length > 1 &&
482+
request.charAt(1) !== '.' &&
483+
request.charAt(1) !== '/' &&
484+
(!isWindows || request.charAt(1) !== '\\'))) {
506485

507-
// Maintain backwards compat with certain broken uses of require('.')
508-
// by putting the module's directory in front of the lookup paths.
509-
if (request === '.') {
510-
if (parent && parent.filename) {
511-
paths.unshift(path.dirname(parent.filename));
512-
} else {
513-
paths.unshift(path.resolve(request));
514-
}
486+
let paths = modulePaths;
487+
if (parent != null && parent.paths && parent.paths.length) {
488+
paths = parent.paths.concat(paths);
515489
}
516490

517491
debug('looking for %j in %j', request, paths);
518492
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
519493
}
520494

521-
// with --eval, parent.id is not set and parent.filename is null
495+
// With --eval, parent.id is not set and parent.filename is null.
522496
if (!parent || !parent.id || !parent.filename) {
523497
// Make require('./path/to/foo') work - normally the path is taken
524498
// from realpath(__filename) but with eval there is no filename

test/parallel/test-require-dot.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ assert.strictEqual(a, b);
1414
process.env.NODE_PATH = fixtures.path('module-require', 'relative');
1515
m._initPaths();
1616

17-
const c = require('.');
18-
assert.strictEqual(
19-
c.value,
20-
42,
21-
`require(".") should honor NODE_PATH; expected 42, found ${c.value}`
17+
assert.throws(
18+
() => require('.'),
19+
{
20+
message: /Cannot find module '\.'/,
21+
code: 'MODULE_NOT_FOUND'
22+
}
2223
);

0 commit comments

Comments
 (0)