diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 3b572bb1e9c..5c17fd5c887 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -20,9 +20,24 @@ const verbose = process.env.DEBUG && process.env.DEBUG.includes('react-native'); +function findCommunityPlatformPackage(spec, startDir = process.cwd()) { + // In monorepos, we cannot make any assumptions on where + // `@react-native-community/*` gets installed. The safest way to find it + // (barring adding an optional peer dependency) is to start from the project + // root. + // + // Note that we're assuming that the current working directory is the project + // root. This is also what `@react-native-community/cli` assumes (see + // https://github.com/react-native-community/cli/blob/14.x/packages/cli-tools/src/findProjectRoot.ts). + const main = require.resolve(spec, {paths: [startDir]}); + return require(main); +} + let android; try { - android = require('@react-native-community/cli-platform-android'); + android = findCommunityPlatformPackage( + '@react-native-community/cli-platform-android', + ); } catch { if (verbose) { console.warn( @@ -33,7 +48,9 @@ try { let ios; try { - ios = require('@react-native-community/cli-platform-ios'); + ios = findCommunityPlatformPackage( + '@react-native-community/cli-platform-ios', + ); } catch { if (verbose) { console.warn(