-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request: Allow specifying lookup paths for require.resolve #5963
Comments
Sorry, I'm not sure I understand your particular problem, is this not something If you don't want to |
If |
_findPaths is an internal method, there is no gurantee regarding it as far as I know. In practice I doubt it'll break but that's not a position I'd be comfortable to have you in (relying on it). Would you mind elaborating a little on why you need to pass the paths inside rather than using module.require ? |
Sure, basically, we have a configuration structure that we build up from multiple sources. At different points during that process, we convert Node.js module names into their paths, and we So, changing just |
FWIW, I'd also like to bless |
Putting aside the Locked status issue for now, I'd also be for it (what @cjihrig said) if we can rename it to something without a Returning to the Locked status, if we have to vote to move it to Stable in the CTC or something, that's a high bar but not an entirely insurmountable obstacle. I certainly appreciate the effort here to document the use case because that should certainly be a pre-requisite for unlocking. |
I think we can expose this functionality too (sans the That said, we should also investigate what exposing it publicly would mean. What happens if someone overrides it with their own hooks? Should that change the internal reference or only further direct calls to it from outside of core? Unlocking a module should require a very high bar. Also - do we have any info on who else is using this from the outside? |
It's not clear, why is this a problem at all. Algorithm is fixed, so implementing it in userland should be possible |
It's fixed in theory. Things do actually change in it from time to time. I think @nzakas has made a pretty compelling argument that people want this behavior. It's already exposed, so I don't think committing to it is a bad idea. |
That's the crux for me. It's already exposed, people are already using it. Exposing "private" methods and expecting people not to use them is something that we should try to correct where we can, and it seems that we can here. |
@Trott the whole module is "private"
We can't just document everything that's exposed. |
True, it's "private". It's also:
Those things taken as a whole seem to suggest that documenting There are other considerations, of course, and this may not be the right path after all. I don't see that as a foregone conclusion. |
A lot of things are exposed.
People use everything they can find
Public APIs are not going to change, and behaviour is not going to change. Stuff like |
I propose adding a second argument to |
@vkurchatkin I'm not familiar with the term "base path" in this context. What I had in mind was to allow an array of lookup paths to be passed, just like |
@nzakas I mean the following: when you call |
@vkurchatkin ah! So that would then calculate what would effectively be |
@nzakas yes, it will resolve paths under the hood |
Just so I'm understanding correctly, would this add those paths to what would already be searched? Or would they replace some of the paths that would have been searched? What I mean is, if the normal lookup is:
Would this change mean A or B below:
|
@vkurchatkin just checking in on this. |
I put together cjihrig@6e72963 as a proof of concept. It allows you pass an array of paths to If there is interest in this, I can add docs and tests, and create a proper PR. |
I'm definitely still interesting in this. :) |
@cjihrig ... I definitely think this is interesting but I'm also definitely sure that there will be resistance extending or changing the existing API. The good news is that everyone is very reluctant to change any part of |
Not sure if it makes a difference, but we got rid of the Locked status entirely. The |
I rebased my branch, https://github.com/cjihrig/node-1/tree/5963, in case anyone wants to discuss this now. |
This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: nodejs/node#5963 Fixes: nodejs/node#16389 PR-URL: nodejs/node#16397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: #5963 Fixes: #16389 PR-URL: #16397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: #5963 Fixes: #16389 PR-URL: #16397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: #5963 Fixes: #16389 PR-URL: #16397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
I don't understand how #16397 resolves (no pun intended) this issue. I've tried doing |
I took a look at your repo. Everything seems to be working as expected with one exception. Opened #17113 to address it. |
Thanks! Just built your PR and the I still don't understand the use case for |
Assume you called |
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: nodejs#5963 PR-URL: nodejs#17113 Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]>
This commit allows custom lookup paths to be passed to require.resolve(). It also adds require.resolve.paths() which retrieves the default resolution paths. Fixes: nodejs/node#5963 Fixes: nodejs/node#16389 PR-URL: nodejs/node#16397 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Wyatt Preul <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]>
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
tl;dr I'd like to propose an additional API be exposed to Node.js JavaScript that allows developers to perform
require.resolve()
, but with an custom list of lookup paths.Background
In ESLint, we allow people to extend their configuration files with Node.js packages, so you can do something like this:
And then ESLint
require
seslint-config-airbnb
. That worked fine until people started creating shareable configs, published on npm, that wanted to inherit from other shareable configs (also published on npm). In that case, they'd want therequire
lookup to happen from their config file and not from the ESLint file doing therequire
.Our Solution
What we ended up doing to get this to work: https://github.com/eslint/eslint/blob/master/lib/util/module-resolver.js
In short:
Module.globalPaths
andmodule.paths
Module._findPath()
to do the lookup, passing the lookup paths arrayThe obvious ugliness here is that we're not just relying on
Module
, but we're relying onModule._findPath()
, which seems to indicate that this method should not be used (assuming the underscore is intended to mean "private".What I'd Like
Some way to get the functionality of
Module._findPath()
that is an official Node.js API that we can rely on without fear of it changing or being removed. Some possible options (definitely not exhaustive):require.resolve()
that allows you to pass an array of lookup paths.Module._findPath()
by creating something likeModule.resolveFromPaths()
that callsModule._findPath()
under the coversOf course, these are just a few ideas I had in my head. Anything that accomplishes the same functionality would be awesome.
Prior Art
It seems like there's a larger need for this capability based on modules available on npm:
resolve
.require.resolve
using CWD as the root. 50,000 downloads in the past month.require.resolve
from any arbitrary path. 1.5 million downloads in the past month.The text was updated successfully, but these errors were encountered: