Skip to content

cli-tools: Use resolver alongside resolveNodeModuleDir #2596

Open
@Downchuck

Description

@Downchuck

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions