Skip to content

Commit 9a2e885

Browse files
committed
Warn once through util.deprecate, refactor
1 parent 712e75c commit 9a2e885

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lib/module.js

+14-7
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ function tryExtensions(p, exts) {
125125
}
126126

127127

128+
const noopDeprecateRequireDot = util.deprecate(function() {},
129+
`warning: require('.') did resolve outside the package directory. ` +
130+
`This functionality will be deprecated soon.`);
131+
132+
128133
Module._findPath = function(request, paths) {
129134
var exts = Object.keys(Module._extensions);
130135

@@ -169,9 +174,8 @@ Module._findPath = function(request, paths) {
169174
}
170175

171176
if (filename) {
172-
if (request === '.' && i > 0) {
173-
console.error(`(node) warning: require('.') resolved to ${filename}`);
174-
}
177+
// Warn once if '.' resolved outside the module dir
178+
if (request === '.' && i > 0) noopDeprecateRequireDot();
175179
Module._pathCache[cacheKey] = filename;
176180
return filename;
177181
}
@@ -215,11 +219,14 @@ Module._resolveLookupPaths = function(request, parent) {
215219
paths = parent.paths.concat(paths);
216220
}
217221

218-
// For '.', put the module's dir at the front of the resolve paths
219-
// TODO(silverwind): Treat '.' exactly the same as './'
222+
// Maintain backwards compat with certain broken uses of require('.')
223+
// by putting the module's directory in front of the lookup paths.
220224
if (request === '.') {
221-
paths.splice(0, 0, parent && parent.filename ?
222-
path.dirname(parent.filename) : path.resolve(request));
225+
if (parent && parent.filename) {
226+
paths.splice(0, 0, path.dirname(parent.filename));
227+
} else {
228+
paths.splice(0, 0, path.resolve(request));
229+
}
223230
}
224231

225232
return [request, paths];

0 commit comments

Comments
 (0)