8

I want to share components of one app with the other and therefore moved them to a separate NPM package. The structure of this package looks like this:

- src
--- components
----- component A
----- component B
- package.json

Some components use other 3rd party libraries and they are listed in the package.json.

Now when installing that shared package to the second project these 3rd party dependencies are not auto linked. For example, when calling pod install they're not installed.

Is it possible somehow install these 3rd party dependencies?

5
  • Did you ever find out how to do this? I ran into the same issue, but there's 0 documentation to be found on this.
    – Anwardo
    Dec 17, 2019 at 14:32
  • @Anwardo unfortunately not. For now, I just added a new README section to the shared package with instructions on what dependencies to install (link) manually. Dec 18, 2019 at 15:08
  • Thanks for your response! After getting really annoyed I ended up doing the same and specifying my dependencies as peerDependencies. Hopefully the RN team will add autolinking for nested dependencies at some point.
    – Anwardo
    Dec 18, 2019 at 15:31
  • Before RN 60 you could use react-native link 'name-of-the-project' .. But now you got -> Unknown dependency. Make sure that the package you are trying to link is already installed in your "node_modules" and present in your "package.json" dependencies.
    – febaisi
    Mar 12, 2020 at 18:50
  • @febaisi I tried that before posting my question but unfortunately, that doesn't help. Mar 14, 2020 at 12:25

1 Answer 1

1

We can patch function findDependencies in @react-native-community/cli as follow to enable auto link dependencies of dependency

function findDependencies(root) {
  let pjson;

  try {
    pjson = JSON.parse(_fs().default.readFileSync(_path().default.join(root, 'package.json'), 'UTF-8'));
  } catch (e) {
    return [];
  }

  const dependencies = Object.keys(pjson.dependencies || []);
  const subDeps = dependencies.reduce(function(accumulator, currentValue) {
    if (!currentValue.includes('your_dependency')) {
      return accumulator;
    }
    const subRoot = `${root}/node_modules/${currentValue}`;
    return accumulator.concat(findDependencies(subRoot));
  }, []);

  const deps = [...Object.keys(pjson.dependencies || {}), ...Object.keys(pjson.devDependencies || {}), ...subDeps];
  return deps;
}
1
  • I don't need to install dependencies like this anymore but when tested on the old repo it worked like a charm :) Thanks! Oct 18, 2020 at 5:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.