require.resolve('./', [options]) with paths option will resolve with root index.js file while it should error with MODULE_NOT_FOUND #23643
Labels
module
Issues and PRs related to the module subsystem.
Folder structure:
index.js
SubFolder/index.js
OtherFolder/main.js
If I run
node index.js
:If I run
node SubFolder/index.js
(expected an error in the last line, not the resolvement):If I run
node OtherFolder/main.js
:I feel the bug is the resolvement of this line
console.log(require.resolve('./', { paths: ['/Users/niksajanjic/Projects/Test/OtherFolder'] }))
insideSubFolder/index.js
into/Users/niksajanjic/Projects/Test/index.js
. That was unexpected for me and I expected it to error out same as runningconsole.log(require.resolve('./'))
insideOtherFolder/main.js
errored out with MODULE_NOT_FOUND. I feel the result of those 2 calls should be exactly the same.This means that if you have a file in the root folder (let's say
main.js
) and if you try to look for the file from A folder into B folder that has the same name as the one in root folder (require.resolve('./main.js', { paths: [ /path/to/folder/B ] })
) it will never error out. Whether you havemain.js
inside B folder or not, the resolvement would still happen and return the path to the file in the root folder. This is very dangerous, this basically means you can't rely on making relative path resolvements inside other paths without the fear of them being resolved to files in the root directory with the same name, and that is a huge probability while using filenames likeindex.js
,main.js
,app.js
orstyle.js
.While we're at this subject, could we benefit from an optional
options
argument insiderequire.resolve.paths(request, options)
so we can see which paths does it resolve to?If this is a bug and not an indended behavior, I could dig it up a bit and debug, maybe even come out with some PR. I know the problem is somewhere inside
Module._resolveFilename
function insideinternal/modules/cjs/loader
file.The text was updated successfully, but these errors were encountered: