Skip to content

Commit 60ce2fd

Browse files
committed
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 <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 083b31d commit 60ce2fd

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

lib/internal/modules/cjs/loader.js

-3
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,6 @@ Module._resolveFilename = function(request, parent, isMain, options) {
584584
fakeParent.paths = Module._nodeModulePaths(path);
585585
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
586586

587-
if (!paths.includes(path))
588-
paths.push(path);
589-
590587
for (var j = 0; j < lookupPaths.length; j++) {
591588
if (!paths.includes(lookupPaths[j]))
592589
paths.push(lookupPaths[j]);

test/fixtures/require-resolve.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ assert.throws(() => {
2424
require.resolve('three')
2525
}, /^Error: Cannot find module 'three'$/);
2626

27-
// However, it can be found if resolution contains the nested index directory.
28-
assert.strictEqual(
29-
require.resolve('three', { paths: [nestedIndex] }),
30-
path.join(nestedIndex, 'three.js')
31-
);
27+
// If the nested-index directory is provided as a resolve path, 'three'
28+
// cannot be found because nested-index is used as a starting point and not
29+
// a searched directory.
30+
assert.throws(() => {
31+
require.resolve('three', { paths: [nestedIndex] })
32+
}, /^Error: Cannot find module 'three'$/);
3233

3334
// Resolution from nested index directory also checks node_modules.
3435
assert.strictEqual(
@@ -50,6 +51,6 @@ assert.throws(() => {
5051
paths.unshift(nestedNodeModules);
5152
assert.strictEqual(
5253
require.resolve('bar', { paths }),
53-
path.join(nestedNodeModules, 'bar.js')
54+
path.join(nodeModules, 'bar.js')
5455
);
5556
}

0 commit comments

Comments
 (0)