Skip to content

Commit 8dcb18d

Browse files
RakaDoankfacebook-github-bot
authored andcommitted
Help Codegen to find library's package.json after failure of importing library's package.json due to missing of ./package.json subpath (#53195)
Summary: This is fix for some React Native libraries can't be found by Codegen, and will make the libraries unusable in new architecture (Turbo Modules) Internally in the Codegen script, it will try to import library's package.json file with the `require.resolve`, but for some React Native libraries will throw an error with `ERR_PACKAGE_PATH_NOT_EXPORTED` code due to using the `exports` field in their package.json file while not exposing the package.json file itself. As an example ```json { "exports": { ".": { "import": { "types": "./lib/typescript/module/index.d.ts", "default": "./lib/module/index.js" }, "require": { "types": "./lib/typescript/commonjs/index.d.ts", "default": "./lib/commonjs/index.js" } }, "./package.json": "./package.json" <-- here some libraries missed this }, "codegenConfig": {} } ``` Personally feel weird that library author has to expose their package.json only for the sake of Codegen and i believe library author shouldn't, even the library consumer don't need it. ## Changelog: <!-- Help reviewers and the release process by writing your own changelog entry. Pick one each for the category and type tags: For more details, see: https://reactnative.dev/contributing/changelogs-in-pull-requests --> [GENERAL] [FIXED] - Help Codegen find library's package.json if some libraries using `exports` field in their package.json file and the `./package.json` subpath is not explicitly defined bypass-github-export-checks Pull Request resolved: #53195 Test Plan: `require.resolve('library/package.json')` [here](https://github.com/facebook/react-native/blob/main/packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js#L203) will throw an error with `ERR_PACKAGE_PATH_NOT_EXPORTED` code by Node.js. So if it does, help Codegen retry to find closest library's package.json with [`require.main.paths`](https://nodejs.org/api/modules.html#requiremain) search paths You can init new app React Native CLI app with my sample react native library here [`ping-react-native`](https://github.com/RakaDoank/ping-react-native) v1.2.2. Due to missing of the `package.json` subpath, before this change, it's autolinked but unusable due to missing of the spec header file. After this change, it works normally. Rollback Plan: Reviewed By: cortinico Differential Revision: D79993649 Pulled By: cipolleschi fbshipit-source-id: fa2bbd6178f5e5fef19a14e67f09ee8a727d01de
1 parent 4fa3c00 commit 8dcb18d

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

  • packages/react-native/scripts/codegen/generate-artifacts-executor

packages/react-native/scripts/codegen/generate-artifacts-executor/utils.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,27 @@ function findExternalLibraries(
204204
paths: [projectRoot],
205205
});
206206
} catch (e) {
207-
// require.resolve fails if the dependency is a local node module.
208207
if (
208+
// require.resolve fails if the `./package.json` subpath is not explicitly defined in the library's `exports` field in its package.json
209+
'code' in e &&
210+
e.code === 'ERR_PACKAGE_PATH_NOT_EXPORTED'
211+
) {
212+
// find the closest library's package.json with the search paths
213+
// $FlowFixMe[prop-missing]
214+
const paths: Array<string> = require.main.paths;
215+
for (const nodeModulesPath of paths) {
216+
const packageJsonFilePath = path.join(
217+
nodeModulesPath,
218+
dependency,
219+
'package.json',
220+
);
221+
if (fs.existsSync(packageJsonFilePath)) {
222+
configFilePath = packageJsonFilePath;
223+
break;
224+
}
225+
}
226+
} else if (
227+
// require.resolve fails if the dependency is a local node module.
209228
dependencies[dependency].startsWith('.') || // handles relative paths
210229
dependencies[dependency].startsWith('/') // handles absolute paths
211230
) {
@@ -214,7 +233,9 @@ function findExternalLibraries(
214233
pkgJson.dependencies[dependency],
215234
'package.json',
216235
);
217-
} else {
236+
}
237+
238+
if (!configFilePath) {
218239
return [];
219240
}
220241
}

0 commit comments

Comments
 (0)