From 03f43042722f1a963b929951f0e397b999583c8b Mon Sep 17 00:00:00 2001 From: Phillip Johnsen Date: Sun, 13 Mar 2016 21:40:38 +0100 Subject: [PATCH 1/2] module: prioritize current dir for local lookups This fixes a bug where a 3rd party module found in node_modules, would be preferred over a ./local module with the same name. Fixes: https://github.com/nodejs/node/issues/5684 PR-URL: https://github.com/nodejs/node/pull/5689 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: Jeremiah Senkpiel --- lib/module.js | 3 +-- test/parallel/test-module-relative-lookup.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-module-relative-lookup.js diff --git a/lib/module.js b/lib/module.js index 74db325dabe276..b992ca8faf3add 100644 --- a/lib/module.js +++ b/lib/module.js @@ -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]; } diff --git a/test/parallel/test-module-relative-lookup.js b/test/parallel/test-module-relative-lookup.js new file mode 100644 index 00000000000000..002ae1a8fb7776 --- /dev/null +++ b/test/parallel/test-module-relative-lookup.js @@ -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'); From fc195406030dc4d61a9c11c558642ff9d28c99a0 Mon Sep 17 00:00:00 2001 From: Myles Borins Date: Sun, 13 Mar 2016 13:59:33 -0700 Subject: [PATCH 2/2] test: update test-repl-require for local paths Currently we are not testing that resolution of local paths is resolved first in the repl. This addition to `test-repl-require` adds an additional fixture an ensures we won't regress in the future PR-URL: https://github.com/nodejs/node/pull/5689 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Colin Ihrig Reviewed-By: Ben Noordhuis Reviewed-By: Jeremiah Senkpiel --- test/fixtures/baz.js | 1 + test/parallel/test-repl-require.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/baz.js diff --git a/test/fixtures/baz.js b/test/fixtures/baz.js new file mode 100644 index 00000000000000..3187340bcbf376 --- /dev/null +++ b/test/fixtures/baz.js @@ -0,0 +1 @@ +module.exports = 'perhaps I work'; diff --git a/test/parallel/test-repl-require.js b/test/parallel/test-repl-require.js index c964951c2cacd8..2638b9643c17ec 100644 --- a/test/parallel/test-repl-require.js +++ b/test/parallel/test-repl-require.js @@ -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'); });