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

cli-tools: Use resolver alongside resolveNodeModuleDir #2596

Open
Downchuck opened this issue Feb 12, 2025 · 0 comments
Open

cli-tools: Use resolver alongside resolveNodeModuleDir #2596

Downchuck opened this issue Feb 12, 2025 · 0 comments

Comments

@Downchuck
Copy link

Downchuck commented Feb 12, 2025

Describe the Feature

The CLI can use resolvers to find node module folders for dependencies in addition to the current search heuristic.

Node.js whether using ESM or CJS has require.resolve or import.meta.resolve -- this technique is already being used in the CLI, just not for the node modules resolution on dependencies. The method simply resolves packageName/package.json then gets the directory name.

Possible Implementations

While ESM may not be relevant, if it is used, the path returned by a resolver is a url,

import {fileURLToPath} from "node:url";

const resolvedUrl = import.meta.resolve("packageName/package.json");
console.log(fileURLToPath(resolvedUrl));

With CJS, and this is code already in the code base:

path.dirname(
      require.resolve('@react-native-community/example/package.json'),
),

A little simpler - as there are packages that define exports and an entry point (such as @react-navigation/bottom-tabs) - using import.meta.resolve("@react-navigation/bottom-tabs") will give the entry point, and one can still traverse up to the nearest package.json file (e.g. package-up).

Context

The find-up usage in findPackageDependencyDir is fine, but the resolver also makes sense, either as the first lookup option or as a fallback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant