react-native-community / cli

The React Native Community CLI - command line tools to help you build RN apps
MIT License
2.36k stars 900 forks source link

autolink doesn't work for libraries which depend on other components with native module #1347

Open chj-damon opened 3 years ago

chj-damon commented 3 years ago

Ask your Question

I build a react-native-library that relies on many other dependencies, like react-native-svgreact-native-image-picker etc. all these dependencies have native modules. library |- react-native-svg |- react-native-image-picker |- rn-fetch-blob |- react-native-reanimated

Now In one project I install my library and run pod install under ios folder, it seems that autolink won't link these native modules from my library dependencies.

what should I do to make it work? I don't want to put all these dependencies in peerDependencies to ask the user to install them manually.

brentvatne commented 3 years ago

there are some cases where autolinking of transitive dependencies could be very useful, but it is not supported at the moment in react-native-cli's autolinking and, even if it were, i would not recommend using it for these specific cases.

  1. the libraries you referred to above are likely to be used directly by a developer outside of the context of your library too, and you developers should include any dependencies that they directly import in their app in their dependencies
  2. if your library installed these dependencies and users also installed them in their package.json, it would be very easy for the versions to go out of sync and lead to multiple versions of the packages being installed. eg: react-native-svg could be under node_modules/react-native-svg and node_modules/your-lib/node_modules/react-native-svg. while this works fine for pure js projects, it does not work for native libraries. you can only link one version of the library, and you will have two versions of the js code.
chj-damon commented 3 years ago

I understand that. But I also what to know what the best way to solve this problem is. If my library is built upon many dependencies that have native modules, I don't want my users to install these dependencies themselves, that is very inconvenient I think.

brentvatne commented 3 years ago

@chj-damon - i agree it's not always ideal! if you have some proposal for how to do it better, then i'd love to hear it! for now, explaining to users that they need to install these other packages is the best you can do. for example: https://reactnavigation.org/docs/getting-started#installing-dependencies-into-a-bare-react-native-project

chj-damon commented 3 years ago

I made an example project under my library project, which I want to use the native module: library |- android |- ios |- src |- example

How can I make it work? I tried to config react-native.config.js:

module.exports = {
  assets: ['./assets/fonts'],
  dependencies: {
    'myLib': {
      root: '../android',
    },
  },
};

it doesn't work.

chj-damon commented 3 years ago

is there any manual about how to build a library that contains native modules, and an example project to test it?

AlenToma commented 2 years ago

I am having the same issue. We should be able to configure those in the library. Like running a script or something too tell the cli to autolink those library.

I cannot think of a reason why you have not solved this yet. There should not be any negative reason to include this as we already add peerDependencies and even if for some reason that it could cause problem if this specified globally, we should be able to run some script or configuration that specify which library should be included in autolink.

github-actions[bot] commented 1 year ago

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

meypod commented 1 year ago

I feel like this is relevant for mono repos as well (I'm using yarn berry) what I'm trying to do is to put all dependencies inside a package (app) and use them across platform (mobile, web) without being able to autolink them, I have to install A LOT with same versions from my "app" package inside each repo, which isn't really convenient