Skip to content
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

Feature Request: method for resolving the directory of a package #42219

Closed
kumavis opened this issue Mar 5, 2022 · 20 comments
Closed

Feature Request: method for resolving the directory of a package #42219

kumavis opened this issue Mar 5, 2022 · 20 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. loaders Issues and PRs related to ES module loaders module Issues and PRs related to the module subsystem. never-stale Mark issue so that it is never considered stale

Comments

@kumavis
Copy link

kumavis commented Mar 5, 2022

What is the problem this feature will solve?

exposing node's module resolution algorithm to utilities

What is the feature you are proposing to solve the problem?

import { resolvePackageDirectory } from 'module';

const xyzDirectoryPath = resolvePackageDirectory('xyz', import.meta.url)
// "/project/node_modules/abc/node_modules/xyz/"

What alternatives have you considered?

the previous trick was

import { dirname } from 'path';
import { createRequire } from 'module';

const require = createRequire(import.meta.url);
// package.json was only reliable location
const packageJsonPath = require.resolve('xyz/package.json');
const xyzDirectoryPath = dirname(packageJsonPath);

but with the introduction of the exports specifier, there is no reliable way to resolve the package's location on disk

@kumavis kumavis added the feature request Issues that request new features to be added to Node.js. label Mar 5, 2022
@kumavis
Copy link
Author

kumavis commented Mar 5, 2022

resolvePackageDirectory can be async if preferred

@kumavis
Copy link
Author

kumavis commented Mar 5, 2022

the internal readPackageScope can be used here

const require = createRequire(import.meta.url);
const rootExportPath = require.resolve('xyz'); // assuming root export
const xyzDirectoryPath = readPackageScope(rootExportPath).path // NOT exposed externally

function readPackageScope(checkPath) {
const rootSeparatorIndex = StringPrototypeIndexOf(checkPath, sep);
let separatorIndex;
do {
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
return false;
const pjson = readPackage(checkPath + sep);
if (pjson) return {
data: pjson,
path: checkPath,
};
} while (separatorIndex > rootSeparatorIndex);
return false;
}

@Trott
Copy link
Member

Trott commented Mar 5, 2022

@nodejs/modules

@devsnek
Copy link
Member

devsnek commented Mar 5, 2022

what would resolvePackageDirectory do when the target is not a folder with a package.json

@devsnek
Copy link
Member

devsnek commented Mar 5, 2022

also do exports actually stop you from resolving package.json files? that seems like an anti-feature.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2022

Duplicate of #49445 / #33460 ?

@ljharb
Copy link
Member

ljharb commented Mar 5, 2022

Yes, exports does stop that. This api is one the modules group was on board to land, but never got built.

@GeoffreyBooth
Copy link
Member

This is a duplicate of #49445 and also discussed in nodejs/loaders#26, but I guess it’s not really a duplicate if it’s in the node repo rather than the others? Dunno how we consider cross-repo duplicates. The plan is to address this as part of nodejs/loaders#26, and you can see an example implementation in the function getPackageType in https://nodejs.org/api/esm.html#transpiler-loader.

@GeoffreyBooth GeoffreyBooth added esm Issues and PRs related to the ECMAScript Modules implementation. loaders Issues and PRs related to ES module loaders module Issues and PRs related to the module subsystem. labels Mar 5, 2022
@ljharb
Copy link
Member

ljharb commented Mar 5, 2022

Is there any reason addressing it needs to wait for loaders to land? There's many use cases for it that don't (and can't) involve loaders.

@aduh95
Copy link
Contributor

aduh95 commented Mar 5, 2022

@ljharb there's no reason to wait for loaders to land, the loaders team has plans to do it in the future but I don't think they would mind if someone else was pushing it forward (quite the contrary), you (or anyone else) are welcome to open a PR to add an API doing this; however such API would have to align with the loader use case – so we don't end up with several slightly different API that don't interop with one another.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2022

Great, as long as the reverse is also true - that anything loaders provides also handles the non-loader use cases.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 2, 2022

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Sep 2, 2022
@ljharb
Copy link
Member

ljharb commented Sep 2, 2022

unstale

@GeoffreyBooth GeoffreyBooth added never-stale Mark issue so that it is never considered stale and removed stale labels Sep 2, 2022
@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2023

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Aug 30, 2023
@ljharb
Copy link
Member

ljharb commented Aug 30, 2023

bump

@bnoordhuis
Copy link
Member

This is basically a duplicate of nodejs/loaders#26, right? If so, I'm inclined to close this as inactionable / blocked-on / whatever and direct people to the loaders issue.

@GeoffreyBooth
Copy link
Member

This is basically a duplicate of nodejs/loaders#26, right? If so, I’m inclined to close this as inactionable / blocked-on / whatever and direct people to the loaders issue.

I don’t know if we want to make issues in other repos canonical. We archive those other repos occasionally, and we wouldn’t want to lose batches of feature requests when we do so.

Yes this is duplicative, but only in the sense that the loaders issue is a proposal for an API to would do a bunch of things including satisfying this request.

@ljharb
Copy link
Member

ljharb commented Aug 30, 2023

I don't see how a loader can satisfy this request, which is runtime resolution long after the program has begun.

@github-actions github-actions bot removed the stale label Aug 31, 2023
@bnoordhuis
Copy link
Member

It doesn't matter to me if it's this issue that stays open or the loaders one but having two issues (and discussions) for the same feature isn't great. I'll close this but feel free to reopen when the other issue is resolved/archived/etc.

To people coming here from search engines, please go here: nodejs/loaders#26

@bnoordhuis bnoordhuis closed this as not planned Won't fix, can't repro, duplicate, stale Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. feature request Issues that request new features to be added to Node.js. loaders Issues and PRs related to ES module loaders module Issues and PRs related to the module subsystem. never-stale Mark issue so that it is never considered stale
Projects
None yet
Development

No branches or pull requests

7 participants