Skip to content
Permalink
Browse files

module: don't search in require.resolve.paths

The paths used by require.resolve() should be treated as
starting points for module resolution, and not actually
searched.

PR-URL: #23683
Fixes: #18408
Refs: #23643
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information...
cjihrig committed Oct 16, 2018
1 parent 083b31d commit 60ce2fd827e4f6e4a8a4f39816f93b3043ec92e1
Showing with 7 additions and 9 deletions.
  1. +0 −3 lib/internal/modules/cjs/loader.js
  2. +7 −6 test/fixtures/require-resolve.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]);
@@ -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')
);
}

0 comments on commit 60ce2fd

Please sign in to comment.
You can’t perform that action at this time.