Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Better module dir detection, add warning
  • Loading branch information
silverwind committed Apr 7, 2015
commit 712e75ce495d1abda1cc399fc653cb58f30dc385
16 changes: 11 additions & 5 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ Module._findPath = function(request, paths) {
}

if (filename) {
if (request === '.' && i > 0) {
console.error(`(node) warning: require('.') resolved to ${filename}`);
}
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Any advice on how to use util.deprecate in this case? It seems util.deprecate is tuned for deprecating functions. In this case, I'd like to print a single warning if this condition is met.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just a thought, and perhaps this is too much of a hack but:

var noopDeprecateRequireDotFile = util.deprecate(function() {}, 'message here')

...

     if (request === '.' && i > 0) noopDeprecateRequireDotFile();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'd be fine with something like that, but in the long term, we probably need something like util.warnOnce(msg). These probably shouldn't be public methods either.

Module._pathCache[cacheKey] = filename;
return filename;
}
Expand Down Expand Up @@ -210,12 +213,15 @@ Module._resolveLookupPaths = function(request, parent) {
if (parent) {
if (!parent.paths) parent.paths = [];
paths = parent.paths.concat(paths);
// For '.', put PWD at the front of the resolve paths
// TODO(silverwind): Treat '.' exactly the same as './'
if (request === '.') {
paths.splice(0, 0, path.resolve(request));
}
}

// For '.', put the module's dir at the front of the resolve paths
// TODO(silverwind): Treat '.' exactly the same as './'
if (request === '.') {
paths.splice(0, 0, parent && parent.filename ?
path.dirname(parent.filename) : path.resolve(request));
}

return [request, paths];
}

Expand Down