diff --git a/lib/module.js b/lib/module.js index cc8d5097bb8..77e5e3fef66 100644 --- a/lib/module.js +++ b/lib/module.js @@ -521,11 +521,14 @@ Module._resolveFilename = function(request, parent, isMain, options) { if (typeof options === 'object' && options !== null && Array.isArray(options.paths)) { + const fakeParent = new Module('', null); + paths = []; for (var i = 0; i < options.paths.length; i++) { const path = options.paths[i]; - const lookupPaths = Module._resolveLookupPaths(path, parent, true); + fakeParent.paths = Module._nodeModulePaths(path); + const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true); if (!paths.includes(path)) paths.push(path); diff --git a/test/fixtures/resolve-paths/default/node_modules/dep/index.js b/test/fixtures/resolve-paths/default/node_modules/dep/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/fixtures/resolve-paths/default/verify-paths.js b/test/fixtures/resolve-paths/default/verify-paths.js new file mode 100644 index 00000000000..dee03fbfe3b --- /dev/null +++ b/test/fixtures/resolve-paths/default/verify-paths.js @@ -0,0 +1,21 @@ +'use strict'; +require('../../../common'); +const assert = require('assert'); +const path = require('path'); + +// By default, resolving 'dep' should return +// fixturesDir/resolve-paths/default/node_modules/dep/index.js. By setting +// the path to fixturesDir/resolve-paths/default, the 'default' directory +// structure should be ignored. + +assert.strictEqual( + require.resolve('dep'), + path.join(__dirname, 'node_modules', 'dep', 'index.js') +); + +const paths = [path.resolve(__dirname, '..', 'defined')]; + +assert.strictEqual( + require.resolve('dep', { paths }), + path.join(paths[0], 'node_modules', 'dep', 'index.js') +); diff --git a/test/fixtures/resolve-paths/defined/node_modules/dep/index.js b/test/fixtures/resolve-paths/defined/node_modules/dep/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/parallel/test-require-resolve.js b/test/parallel/test-require-resolve.js index 6f2253a4e27..4fbf697faf5 100644 --- a/test/parallel/test-require-resolve.js +++ b/test/parallel/test-require-resolve.js @@ -37,3 +37,4 @@ assert.strictEqual('path', require.resolve('path')); // Test configurable resolve() paths. require(fixtures.path('require-resolve.js')); +require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));