Skip to content

Commit 2f512e3

Browse files
cjihrigtargos
authored andcommitted
module: handle relative paths in resolve paths
This commit adds support for relative paths in require.resolve()'s paths option. PR-URL: #27598 Fixes: #27583 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent a23e86f commit 2f512e3

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

lib/internal/modules/cjs/loader.js

+16-9
Original file line numberDiff line numberDiff line change
@@ -575,18 +575,25 @@ Module._resolveFilename = function(request, parent, isMain, options) {
575575

576576
if (typeof options === 'object' && options !== null &&
577577
Array.isArray(options.paths)) {
578-
const fakeParent = new Module('', null);
578+
const isRelative = request.startsWith('./') || request.startsWith('../') ||
579+
(isWindows && request.startsWith('.\\') || request.startsWith('..\\'));
579580

580-
paths = [];
581+
if (isRelative) {
582+
paths = options.paths;
583+
} else {
584+
const fakeParent = new Module('', null);
581585

582-
for (var i = 0; i < options.paths.length; i++) {
583-
const path = options.paths[i];
584-
fakeParent.paths = Module._nodeModulePaths(path);
585-
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
586+
paths = [];
586587

587-
for (var j = 0; j < lookupPaths.length; j++) {
588-
if (!paths.includes(lookupPaths[j]))
589-
paths.push(lookupPaths[j]);
588+
for (var i = 0; i < options.paths.length; i++) {
589+
const path = options.paths[i];
590+
fakeParent.paths = Module._nodeModulePaths(path);
591+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
592+
593+
for (var j = 0; j < lookupPaths.length; j++) {
594+
if (!paths.includes(lookupPaths[j]))
595+
paths.push(lookupPaths[j]);
596+
}
590597
}
591598
}
592599
} else {

test/fixtures/require-resolve.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
require('../common');
2+
const common = require('../common');
33
const assert = require('assert');
44
const path = require('path');
55
const nodeModules = path.join(__dirname, 'node_modules');
@@ -54,3 +54,33 @@ assert.throws(() => {
5454
path.join(nodeModules, 'bar.js')
5555
);
5656
}
57+
58+
// Verify that relative request paths work properly.
59+
{
60+
const searchIn = './' + path.relative(process.cwd(), nestedIndex);
61+
62+
// Search in relative paths.
63+
assert.strictEqual(
64+
require.resolve('./three.js', { paths: [searchIn] }),
65+
path.join(nestedIndex, 'three.js')
66+
);
67+
68+
// Search in absolute paths.
69+
assert.strictEqual(
70+
require.resolve('./three.js', { paths: [nestedIndex] }),
71+
path.join(nestedIndex, 'three.js')
72+
);
73+
74+
// Repeat the same tests with Windows slashes in the request path.
75+
if (common.isWindows) {
76+
assert.strictEqual(
77+
require.resolve('.\\three.js', { paths: [searchIn] }),
78+
path.join(nestedIndex, 'three.js')
79+
);
80+
81+
assert.strictEqual(
82+
require.resolve('.\\three.js', { paths: [nestedIndex] }),
83+
path.join(nestedIndex, 'three.js')
84+
);
85+
}
86+
}

0 commit comments

Comments
 (0)