React-native: Question: How to auto link dependencies of a dependency?

Created on 21 Nov 2019  路  5Comments  路  Source: facebook/react-native

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?

Ran Commands Question

All 5 comments


We are automatically closing this issue because it does not appear to follow any of the provided issue templates.

馃憠 Click here if you want to report a reproducible bug or regression in React Native.

@chika-kasymov Have you found any solutions for this?

Hey @hamidhadi, unfortunately not. For now, I just updated the README of that package with a list of dependencies to install as a prerequisite.

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;
}

@phamducgiam I don't need to install dependencies like this anymore but when tested on the old repo the patch worked like a charm :) Thanks!

Was this page helpful?
0 / 5 - 0 ratings