You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Does the require.resolve paths option work just like the baseDir option? #188
Since Node.js v8.9.0, a new paths option was made available to the core Node.js require.resolve function:
paths<string[]> Paths to resolve module location from. If present, these paths are used instead of the default resolution paths, with the exception of GLOBAL_FOLDERS like $HOME/.node_modules, which are always included. Note that each of these paths is used as a starting point for the module resolution algorithm, meaning that the node_modules hierarchy is checked from this location.
As far as I can see, if just given a single element in the paths array, this works identical to the baseDir option of this module. The new paths features does however take into account the GLOBAL_FOLDERS, which I'm not sure if this module does?
I ask because I would like to feature detect this and use the internal Node.js function if available, but fall back to this module if not. But I wasn't sure if the behavior was 100% identical 🤔
I don't personally use GLOBAL_FOLDERS no, but I'm the maintainer of a popular module which is used to patch the require function where I depend on being able to resolve modules. And I can't rule out if any of their apps depend on the GLOBAL_FOLDERS environment variable unfortunately.
I somehow can't get the normal test suite to run in master. Is it because I have cloned this repo into a folder whos path already contain a node_modules folder? Usually that's where I keep my all the Node modules I'm working on which makes working on multiple modules with inter-dependencies much easier.
resolve definitely does not mimic require.resolve behavior in terms of paths option. In require.resolve it works as multiple basedir. But also require includes there dirs from NODE_PATH which is rarely desired behavior.
Activity
ljharb commentedon Mar 20, 2019
Since it's relatively new, this package hasn't adapted to it yet, no.
Would you be willing to make a PR with test cases, comparing require.resolve's paths option to the basedir option?
Another possibility might be, adding a
pathsoption toresolveitself.(do you need the GLOBAL_FOLDERS? requiring things from there is widely considered an antipattern)
watson commentedon Mar 21, 2019
I don't mind taking a stab at it.
I don't personally use
GLOBAL_FOLDERSno, but I'm the maintainer of a popular module which is used to patch therequirefunction where I depend on being able to resolve modules. And I can't rule out if any of their apps depend on theGLOBAL_FOLDERSenvironment variable unfortunately.watson commentedon Mar 31, 2019
I somehow can't get the normal test suite to run in
master. Is it because I have cloned this repo into a folder whos path already contain anode_modulesfolder? Usually that's where I keep my all the Node modules I'm working on which makes working on multiple modules with inter-dependencies much easier.watson commentedon Mar 31, 2019
Installed in
/tmpand it worked. So I guess it's related to thenode_modulesfolder in the pathwatson commentedon Mar 31, 2019
As far as I can see, no where in the current tests do you validate that
require.syncreturns the same result asrequire.resolve. Is this on purpose?ljharb commentedon Apr 6, 2019
Not that I know of; I've always found that a bit confusing but haven't taken the time to fix it.
It sounds like there's a few action items here:
node_modules(if i've understood the problem correctly)resolveandresolve.syncwithrequire.resolve, to ensure consistency and/or illustrate the differencespathsoptionThoughts?
wclr commentedon Aug 24, 2020
resolvedefinitely does not mimicrequire.resolvebehavior in terms ofpathsoption. Inrequire.resolveit works as multiplebasedir. But also require includes there dirs fromNODE_PATHwhich is rarely desired behavior.ljharb commentedon Aug 25, 2020
@watson are you interested in doing any of the steps I outlined above?
[Tests] add tests comparing `resolve.sync` to `require.resolve`
[Tests] add tests comparing `resolve.sync` to `require.resolve`
[Tests] add tests comparing `resolve.sync` to `require.resolve`
ljharb commentedon Jan 4, 2022
I've added bullet point 2 in 067db38.
If you could give me some examples of test cases for
pathswith more than one item, i can look into supporting it inresolve.[Tests] add tests comparing `resolve.sync` to `require.resolve`
JounQin commentedon May 28, 2025
I found a Node.js potential regression starts from Node 12.
nodejs/node#58487