module: handle relative paths in resolve paths
This commit adds support for relative paths in require.resolve()'s paths option. PR-URL: https://github.com/nodejs/node/pull/27598 Fixes: https://github.com/nodejs/node/issues/27583 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
c30ef3cbd2
commit
dac1143fc8
@ -575,6 +575,12 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
|||||||
|
|
||||||
if (typeof options === 'object' && options !== null &&
|
if (typeof options === 'object' && options !== null &&
|
||||||
Array.isArray(options.paths)) {
|
Array.isArray(options.paths)) {
|
||||||
|
const isRelative = request.startsWith('./') || request.startsWith('../') ||
|
||||||
|
(isWindows && request.startsWith('.\\') || request.startsWith('..\\'));
|
||||||
|
|
||||||
|
if (isRelative) {
|
||||||
|
paths = options.paths;
|
||||||
|
} else {
|
||||||
const fakeParent = new Module('', null);
|
const fakeParent = new Module('', null);
|
||||||
|
|
||||||
paths = [];
|
paths = [];
|
||||||
@ -589,6 +595,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
|
|||||||
paths.push(lookupPaths[j]);
|
paths.push(lookupPaths[j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
paths = Module._resolveLookupPaths(request, parent);
|
paths = Module._resolveLookupPaths(request, parent);
|
||||||
}
|
}
|
||||||
|
32
test/fixtures/require-resolve.js
vendored
32
test/fixtures/require-resolve.js
vendored
@ -1,5 +1,5 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
require('../common');
|
const common = require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const nodeModules = path.join(__dirname, 'node_modules');
|
const nodeModules = path.join(__dirname, 'node_modules');
|
||||||
@ -54,3 +54,33 @@ assert.throws(() => {
|
|||||||
path.join(nodeModules, 'bar.js')
|
path.join(nodeModules, 'bar.js')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Verify that relative request paths work properly.
|
||||||
|
{
|
||||||
|
const searchIn = './' + path.relative(process.cwd(), nestedIndex);
|
||||||
|
|
||||||
|
// Search in relative paths.
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('./three.js', { paths: [searchIn] }),
|
||||||
|
path.join(nestedIndex, 'three.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Search in absolute paths.
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('./three.js', { paths: [nestedIndex] }),
|
||||||
|
path.join(nestedIndex, 'three.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Repeat the same tests with Windows slashes in the request path.
|
||||||
|
if (common.isWindows) {
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('.\\three.js', { paths: [searchIn] }),
|
||||||
|
path.join(nestedIndex, 'three.js')
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.strictEqual(
|
||||||
|
require.resolve('.\\three.js', { paths: [nestedIndex] }),
|
||||||
|
path.join(nestedIndex, 'three.js')
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user