diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js index e217b99fcb0a..e833a5921620 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js @@ -206,6 +206,26 @@ function findExternalLibraries( } catch (e) { // require.resolve fails if the dependency is a local node module. if ( + // require.resolve fails if the `./package.json` subpath is not explicitly defined in the library's `exports` field in its package.json + 'code' in e && + e.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED' + ) { + // find the closest library's package.json with the search paths + // $FlowFixMe[prop-missing] + const paths /*: Array*/ = require.main.paths; + for (const nodeModulesPath of paths) { + const packageJsonFilePath = path.join( + nodeModulesPath, + dependency, + 'package.json', + ); + if (fs.existsSync(packageJsonFilePath)) { + configFilePath = packageJsonFilePath; + break; + } + } + } else if ( + // require.resolve fails if the dependency is a local node module. dependencies[dependency].startsWith('.') || // handles relative paths dependencies[dependency].startsWith('/') // handles absolute paths ) {