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: prioritize current directory for local lookup #5689

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,7 @@ Module._resolveLookupPaths = function(request, parent) {
if (!parent || !parent.id || !parent.filename) {
// make require('./path/to/foo') work - normally the path is taken
// from realpath(__filename) but with eval there is no filename
var mainPaths = ['.'].concat(modulePaths);
mainPaths = Module._nodeModulePaths('.').concat(mainPaths);
var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
return [request, mainPaths];
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/baz.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'perhaps I work';
10 changes: 10 additions & 0 deletions test/parallel/test-module-relative-lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

require('../common');
const assert = require('assert');
const _module = require('module'); // avoid collision with global.module
const lookupResults = _module._resolveLookupPaths('./lodash');
const paths = lookupResults[1];

assert.strictEqual(paths[0], '.',
'Current directory is prioritized before node_modules for local modules');
4 changes: 2 additions & 2 deletions test/parallel/test-repl-require.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ server.listen(options, function() {
const conn = net.connect(options);
conn.setEncoding('utf8');
conn.on('data', (data) => answer += data);
conn.write('require("baz")\n.exit\n');
conn.write('require("baz")\nrequire("./baz")\n.exit\n');
});

process.on('exit', function() {
assert.strictEqual(false, /Cannot find module/.test(answer));
assert.strictEqual(false, /Error/.test(answer));
assert.strictEqual(true, /eye catcher/.test(answer));
assert.strictEqual(answer, '\'eye catcher\'\n\'perhaps I work\'\n');
});