-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
add API for "package dir" #49445
Comments
How would the resolver source be determined? Maybe
|
Yes, that is true. It would need to do the searching contextually, just like |
As paths are URLs in ESM, "dir" is perhaps not the best name. But aside from naming, yes, this would be a good method to expose. In #33460 (comment) I had suggested |
I'm removing the modules agenda label here, as the discussion around this seems to be the same topic as #33460 which is still on the agenda as well. |
Any plans to resolve this? |
That seems a bit overkill. All that I think is needed is I haven't yet had time to make a PR, but anyone is welcome to beat me to it. |
I also am unclear on how any API should integrate with redirection mechanisms like policies and import maps. I'd personally be fine ignoring redirection mechanisms but that could lead to problems where importing doesn't match the API. |
We could add a method to the |
Or a function that takes two parameters. |
I’d indeed expect a second argument to the static method that took __dirname/import.meta.url. Dangling it off require wouldn’t work in ESM. I’m not sure how it would interact with policies, but i don’t think there’s any security issues from retrieving a path - if filesystem access is restricted, then you’d be unable to use it, which policies could dictate? |
@ljharb I am unclear / don't see an issue at a glance, but the problem can be explained by example: // redirected to file:///app/node_modules/apm/interceptions/sql.js instead of file:///app/node_modules/sql/index.js
import.meta.resolve('sql');
// likely want file:///app/node_modules/sql/ (trailing slash, yes) ? but that now doesn't match where it is actually getting loaded from
module.package('sql'); This would affect pretty much any custom resolver the more I think about it, but we tend to state for policies that it is the job of the policy creator to manually enforce things like |
Makes perfect sense. Theoretically, this api in node could use the same resolver that would respect policies, and could return the directory that contains that package's The double argument form, with source location, was always the plan; i just failed to mention the second argument in my above comment. |
@ljharb if it respects policies, are you saying it should return |
It should return whatever path |
@ljharb I'm asking which |
That of the |
@ljharb ah, |
precise naming aside, that's the only API we've ever really discussed or expected. |
I just published a pretty naive solution for this implemented in user-land here: https://www.npmjs.com/package/package-resolver Doing this implies that we don't respect the redirects set by policies. Do you all think this could be a good idea? |
It’s a good basis to start, but i think that to land it needs to precisely match what require does. |
That would be my guess as well. @MylesBorins what do you think? |
It seems like it won't exactly match |
@MylesBorins what is your opinion here, is it okay to ship an API that does not respect policies? |
Currently, no. Either way is fine, but I'd prefer we not make it align with |
I think module features should always align with require and import, and when require/import aligns with policies, this should too. If those are inconsistent with alignment to policies, that seems like a bug. |
@ljharb I can make them align. If we do though, the situation described above doesn't seem to be what you want? |
It seems like what we should do is kick off a PR to core and work these out there. I think if we mark this as an experimental API we could always land something with a warning and gather feedback |
@bmeck basically the entire purpose of this API is to provide a reliable replacement for |
@ljharb ok, can do |
I would add... if we add this API as experimental we could align everything (import / require / resolve / this api) as a single push... I don't think we should block an initial implementation on a larger platform inconsistency |
This comment has been minimized.
This comment has been minimized.
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the
never-stale
|
never stale. |
See #33460.
tl;dr: it turns out that there's a number of tools that need to be able to locate a package's root directory, and with "exports", they can no longer rely on
package/package.json
working, norpackage/
.If we could provide this API, then CJS could
path.join
it, and ESM could useURL
, to get whatever file the tool needs tofs.readFile
.Adding this new API would bypass questions about "should
package.json
be an implicit part of a package's API", as well as avoid reliance on ESM-only or CJS-only mechanisms. By providing the package root dir rather than itspackage.json
, we would not be encouraging or discouraging any patterns of "where to store metadata" - instead, we'd be correctly leaving that choice up to userland.Example solution:
module.packageDir(specifier)
andmodule.packageDirSync(specifier)
(sync access is critical for many use cases).The text was updated successfully, but these errors were encountered: