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: 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 <jasnell@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
parent
083b31d850
commit
60ce2fd827
@ -584,9 +584,6 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
|||||||
fakeParent.paths = Module._nodeModulePaths(path);
|
fakeParent.paths = Module._nodeModulePaths(path);
|
||||||
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
|
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
|
||||||
|
|
||||||
if (!paths.includes(path))
|
|
||||||
paths.push(path);
|
|
||||||
|
|
||||||
for (var j = 0; j < lookupPaths.length; j++) {
|
for (var j = 0; j < lookupPaths.length; j++) {
|
||||||
if (!paths.includes(lookupPaths[j]))
|
if (!paths.includes(lookupPaths[j]))
|
||||||
paths.push(lookupPaths[j]);
|
paths.push(lookupPaths[j]);
|
||||||
|
13
test/fixtures/require-resolve.js
vendored
13
test/fixtures/require-resolve.js
vendored
@ -24,11 +24,12 @@ assert.throws(() => {
|
|||||||
require.resolve('three')
|
require.resolve('three')
|
||||||
}, /^Error: Cannot find module 'three'$/);
|
}, /^Error: Cannot find module 'three'$/);
|
||||||
|
|
||||||
// However, it can be found if resolution contains the nested index directory.
|
// If the nested-index directory is provided as a resolve path, 'three'
|
||||||
assert.strictEqual(
|
// cannot be found because nested-index is used as a starting point and not
|
||||||
require.resolve('three', { paths: [nestedIndex] }),
|
// a searched directory.
|
||||||
path.join(nestedIndex, 'three.js')
|
assert.throws(() => {
|
||||||
);
|
require.resolve('three', { paths: [nestedIndex] })
|
||||||
|
}, /^Error: Cannot find module 'three'$/);
|
||||||
|
|
||||||
// Resolution from nested index directory also checks node_modules.
|
// Resolution from nested index directory also checks node_modules.
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
@ -50,6 +51,6 @@ assert.throws(() => {
|
|||||||
paths.unshift(nestedNodeModules);
|
paths.unshift(nestedNodeModules);
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
require.resolve('bar', { paths }),
|
require.resolve('bar', { paths }),
|
||||||
path.join(nestedNodeModules, 'bar.js')
|
path.join(nodeModules, 'bar.js')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user