From 60ce2fd827e4f6e4a8a4f39816f93b3043ec92e1 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 15 Oct 2018 23:36:56 -0400 Subject: [PATCH] module: don't search in require.resolve.paths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The paths used by require.resolve() should be treated as starting points for module resolution, and not actually searched. PR-URL: https://github.com/nodejs/node/pull/23683 Fixes: https://github.com/nodejs/node/issues/18408 Refs: https://github.com/nodejs/node/issues/23643 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Matteo Collina --- lib/internal/modules/cjs/loader.js | 3 --- test/fixtures/require-resolve.js | 13 +++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 0fbb3b5cf35a79..91d32ea2d06fb1 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -584,9 +584,6 @@ Module._resolveFilename = function(request, parent, isMain, options) { fakeParent.paths = Module._nodeModulePaths(path); const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true); - if (!paths.includes(path)) - paths.push(path); - for (var j = 0; j < lookupPaths.length; j++) { if (!paths.includes(lookupPaths[j])) paths.push(lookupPaths[j]); diff --git a/test/fixtures/require-resolve.js b/test/fixtures/require-resolve.js index 982b15c4979c8c..2a0dc8846ce3cb 100644 --- a/test/fixtures/require-resolve.js +++ b/test/fixtures/require-resolve.js @@ -24,11 +24,12 @@ assert.throws(() => { require.resolve('three') }, /^Error: Cannot find module 'three'$/); - // However, it can be found if resolution contains the nested index directory. - assert.strictEqual( - require.resolve('three', { paths: [nestedIndex] }), - path.join(nestedIndex, 'three.js') - ); + // If the nested-index directory is provided as a resolve path, 'three' + // cannot be found because nested-index is used as a starting point and not + // a searched directory. + assert.throws(() => { + require.resolve('three', { paths: [nestedIndex] }) + }, /^Error: Cannot find module 'three'$/); // Resolution from nested index directory also checks node_modules. assert.strictEqual( @@ -50,6 +51,6 @@ assert.throws(() => { paths.unshift(nestedNodeModules); assert.strictEqual( require.resolve('bar', { paths }), - path.join(nestedNodeModules, 'bar.js') + path.join(nodeModules, 'bar.js') ); }