Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: restore and warn on require('.') usage with NODE_PATH #1363

Closed
wants to merge 6 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function tryExtensions(p, exts) {
}


const noopDeprecateRequireDot = util.deprecate(function() {},
`warning: require('.') did resolve outside the package directory. ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the use of a template string seems unnecessary here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, "did resolve" could be just "resolved"

`This functionality will be deprecated soon.`);


Module._findPath = function(request, paths) {
var exts = Object.keys(Module._extensions);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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];
}

Expand Down