Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit caff930

Browse filesBrowse files
cjihrigMylesBorins
authored andcommitted
module: replace default paths in require.resolve()
Prior to this commit, the default search paths would be included in the require.resolve() process, even if user specified paths were provided. This commit causes the default paths to be omitted by using a fake parent module. Refs: #5963 PR-URL: #17113 Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9c0c336 commit caff930
Copy full SHA for caff930

File tree

Expand file treeCollapse file tree

5 files changed

+26
-1
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+26
-1
lines changed
Open diff view settings
Collapse file

‎lib/module.js‎

Copy file name to clipboardExpand all lines: lib/module.js
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,14 @@ Module._resolveFilename = function(request, parent, isMain, options) {
518518

519519
if (typeof options === 'object' && options !== null &&
520520
Array.isArray(options.paths)) {
521+
const fakeParent = new Module('', null);
522+
521523
paths = [];
522524

523525
for (var i = 0; i < options.paths.length; i++) {
524526
const path = options.paths[i];
525-
const lookupPaths = Module._resolveLookupPaths(path, parent, true);
527+
fakeParent.paths = Module._nodeModulePaths(path);
528+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
526529

527530
if (!paths.includes(path))
528531
paths.push(path);
Collapse file

‎test/fixtures/resolve-paths/default/node_modules/dep/index.js‎

Copy file name to clipboardExpand all lines: test/fixtures/resolve-paths/default/node_modules/dep/index.js
Whitespace-only changes.
Collapse file
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
require('../../../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
// By default, resolving 'dep' should return
7+
// fixturesDir/resolve-paths/default/node_modules/dep/index.js. By setting
8+
// the path to fixturesDir/resolve-paths/default, the 'default' directory
9+
// structure should be ignored.
10+
11+
assert.strictEqual(
12+
require.resolve('dep'),
13+
path.join(__dirname, 'node_modules', 'dep', 'index.js')
14+
);
15+
16+
const paths = [path.resolve(__dirname, '..', 'defined')];
17+
18+
assert.strictEqual(
19+
require.resolve('dep', { paths }),
20+
path.join(paths[0], 'node_modules', 'dep', 'index.js')
21+
);
Collapse file

‎test/fixtures/resolve-paths/defined/node_modules/dep/index.js‎

Copy file name to clipboardExpand all lines: test/fixtures/resolve-paths/defined/node_modules/dep/index.js
Whitespace-only changes.
Collapse file

‎test/parallel/test-require-resolve.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-require-resolve.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ assert.strictEqual('path', require.resolve('path'));
3737

3838
// Test configurable resolve() paths.
3939
require(fixtures.path('require-resolve.js'));
40+
require(fixtures.path('resolve-paths', 'default', 'verify-paths.js'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.