From 9ba96ad79d62a77dbf12b0012eaa4f61e4749ec0 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Fri, 14 Feb 2025 04:56:55 -0800 Subject: [PATCH] fix: fix `@react-native-community/cli` not being found in pnpm setups (#47304) Summary: Fix `react-native-community/cli` not being found in pnpm setups ## Changelog: [GENERAL] [FIXED] - Fix `react-native-community/cli` not being found in pnpm setups Pull Request resolved: https://github.com/facebook/react-native/pull/47304 Test Plan: 1. Clone/check out this branch: https://github.com/microsoft/rnx-kit/pull/3409 2. Run `yarn react-native config` Reviewed By: cortinico Differential Revision: D65209065 Pulled By: robhogan fbshipit-source-id: 2ceb73ad140b4afe193e879779c2d8a4b9adf3fc --- packages/react-native/cli.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/react-native/cli.js b/packages/react-native/cli.js index 6d6751e7a30..4806150d372 100755 --- a/packages/react-native/cli.js +++ b/packages/react-native/cli.js @@ -23,6 +23,15 @@ const deprecated = () => { ); }; +function findCommunityCli(startDir = process.cwd()) { + // With isolated node_modules (eg pnpm), we won't be able to find + // `@react-native-community/cli` starting from the `react-native` directory. + // Instead, we should use the project root, which we assume to be the cwd. + const options = {paths: [startDir]}; + const rncli = require.resolve('@react-native-community/cli', options); + return require(rncli); +} + function isMissingCliDependency(error) { return ( error.code === 'MODULE_NOT_FOUND' && @@ -217,7 +226,7 @@ async function main() { } try { - return require('@react-native-community/cli').run(name); + return findCommunityCli().run(name); } catch (e) { if (isMissingCliDependency(e)) { warnWithExplicitDependency(); @@ -231,7 +240,7 @@ if (require.main === module) { main(); } else { try { - cli = require('@react-native-community/cli'); + cli = findCommunityCli(); } catch (e) { // We silence @react-native-community/cli missing as it is no // longer a dependency