How do I integrate a native module as a dependency in a custom module/library in react native

1.7k Views Asked by At

I created a module/library with react-native-create-library for handling specific ble procedures of a custom device and chose react-native-ble-manager for the native ble functionalities. Since I did not write any native code in my library I'm just exporting my module in index.js

import BleHandler from './src/BleHandler.js';

export default BleHandler;

Then I packed my library with npm to locally test it in a test application. Up until here everything is fine and I can import and use my module in the test application. But when I try to initialize the ble module it fails because it appearently is not contained in NativeModules:

[TypeError: null is not an object (evaluating 'bleManager.start')]

I included the ble module in the package.json of my module

"dependencies": {
    "@react-native-community/async-storage": "^1.11.0",
    "react": "16.13.1",
    "react-native": "0.63.3",
    "react-native-ble-manager": "^7.4.1",
    "react-native-fs": "^2.16.6",
    "react-native-windows": "^0.57.2"
  }

But when I checked in:
TestApplication/node_modules/my-module/node_modules
react-native-ble-manager was not there.

My question now is, if it is even possible to use a native module inside a module, and if so, how to do it correctly?

1

There are 1 best solutions below

0
On

You should add react-native-ble-manager to peerDependencies in your library instead of in dependencies. Then the user of your library would install the native module in their project under dependencies.

See https://github.com/callstack/react-native-builder-bob#how-do-i-add-a-react-native-library-containing-native-code-as-a-dependency-in-my-library